473,838 Members | 1,706 Online

# Programming Puzzle

I found these questions on a web site and wish to share with all of u
out there,Can SomeOne Solve these Porgramming puzzles.
Programming Puzzles

Some companies certainly ask for these things. Specially Microsoft.
Here are my favorite puzzles. Don't send me emails asking for the
solutions.

Q1 Write a "Hello World" program in 'C' without using a semicolon.
Q2 Write a C++ program without using any loop (if, for, while etc) to
print numbers from 1 to 100 and 100 to 1;
Q3 C/C++ : Exchange two numbers without using a temporary variable.
Q4 C/C++ : Find if the given number is a power of 2.
Q5 C/C++ : Multiply x by 7 without using multiplication (*) operator.
Q6 C/C++ : Write a function in different ways that will return f(7) =
4 and f(4) = 7
Q7 Remove duplicates in array
Q8 Finding if there is any loop inside linked list.
Q9 Remove duplicates in an no key access database without using an
array
Q10 Write a program whose printed output is an exact copy of the
source. Needless to say, merely echoing the actual source file is not
allowed.
Q11 From a 'pool' of numbers (four '1's, four '2's .... four '6's),
each player selects a number and adds it to the total. Once a number
is used, it must be removed from the pool. The winner is the person
whose number makes the total equal 31 exactly.
Q12 Swap two numbers without using a third variable.
Given an array (group) of numbers write all the possible sub groups of
this group.
Q14 Convert (integer) number in binary without loops.

Q3,12 are similar , Q7 is simple & I know there answer For the Rest
Nov 14 '05
271 20374
In message <zo************ ****@newssvr27. news.prodigy.co m>, Mabden
<mabden@sbc_glo bal.net> writes
"Foobarius Frobinium" <fo******@youre mailbox.com> wrote in message
news:a7******* *************** ****@posting.go ogle.com...
js*******@sanch arnet.in (Jatinder) wrote in message

news:<22****** *************** *****@posting.g oogle.com>...
You mentioned MS often use these sorts of puzzles to test their
programmers. I got an even better one to stump even the best MS
programmers:
#include <stdio.h>

int main(void) {
int *foo = NULL;

fprintf(stdout, "%i\n", *foo);
}

What will happen if I compile and run this program??

You'll get a warning saying main() has no return value.

Not on this side of the crossposting ;-)

--
Richard Herring
Nov 14 '05 #61
JKop wrote:
Unsigned int variables have better-defined overflow characteristics
than hypothetical water-bucket variables.

This I don't even want to understand.

If you're telling me that unsigned ints overflow in nice eloquent way, then
my only conclusion is that there's an amorphous blob of memory being wasted.

Unsigned integer types have a well defined behaviour both in C and C++.
If the variable gets increased past the maximum value then it wraps
around (begins from 0 again).

Regards,

Ioannis Vranos
Nov 14 '05 #62
JKop wrote:
Unsigned int variables have better-defined overflow characteristics
than hypothetical water-bucket variables.

This I don't even want to understand.

If you're telling me that unsigned ints overflow in nice eloquent way, then
my only conclusion is that there's an amorphous blob of memory being wasted.

Unsigned integer types have a well defined behaviour both in C and C++.
If the variable gets increased past the maximum value then it wraps
around (begins from 0 again).

Regards,

Ioannis Vranos
Nov 14 '05 #63
Ioannis Vranos posted:
Unsigned integer types have a well defined behaviour both in C and C++.
If the variable gets increased past the maximum value then it wraps
around (begins from 0 again).

Okay, then it depends on one's definition of "over-flow".

Consider (on an 8-Bit char system):
unsigned char a = 200;

unsigned char b = 100;

unsigned char c = ( a + b );
I'd call that over-flow, ie. you want it to be 300, but the storage system
can't accomodate it.
Let's try swap the values of a and b without a temp variable:
int main()
{
unsigned char a = 200;

unsigned char b = 100;

a = a + b; // a == 300, b == 100

b = a - b; // a == 300, b == 200

a = a - b; // a == 100, b == 200

}
That's what'll happen it the beautiful land of jelly, where nobody gets sick
and everyone stays young for ever.

Here's what'll really happen:

int main()
{
unsigned char a = 200;

unsigned char b = 100;

a = a + b // a == 45, b == 100

b = a - b // a == 200, b == 100

a = a - b // a == 100, b == 100

}
Now, as far as I know, if you switch

a == 200, b == 100
you don't get:

a == 100, b == 100
But that's just human observation.
-JKop
Nov 14 '05 #64
JKop 2004-06-28 :
Ioannis Vranos posted:
Unsigned integer types have a well defined behaviour both in C and C++.
If the variable gets increased past the maximum value then it wraps
around (begins from 0 again).

Okay, then it depends on one's definition of "over-flow".

Consider (on an 8-Bit char system):
unsigned char a = 200;

unsigned char b = 100;

unsigned char c = ( a + b );
I'd call that over-flow, ie. you want it to be 300, but the storage system
can't accomodate it.
Let's try swap the values of a and b without a temp variable:
int main()
{
unsigned char a = 200;

unsigned char b = 100;

a = a + b; // a == 300, b == 100

b = a - b; // a == 300, b == 200

a = a - b; // a == 100, b == 200

}
That's what'll happen it the beautiful land of jelly, where nobody gets sick
and everyone stays young for ever.

Here's what'll really happen:

int main()
{
unsigned char a = 200;

unsigned char b = 100;

a = a + b // a == 45, b == 100

b = a - b // a == 200, b == 100

a = a - b // a == 100, b == 100

}
Now, as far as I know, if you switch

a == 200, b == 100
you don't get:

a == 100, b == 100
But that's just human observation.

Jkop, you didn't observe anything.
I must tell you: you forgot to switch on your brain before writing,
as usual. Please don't fill this newsgroup with useless, wrong and

Walter Tross
Nov 14 '05 #65
In message <WB************ *****@news.indi go.ie>, JKop <NU**@NULL.NULL >
writes
Ioannis Vranos posted:
Unsigned integer types have a well defined behaviour both in C and C++.
If the variable gets increased past the maximum value then it wraps
around (begins from 0 again).

Okay, then it depends on one's definition of "over-flow".

Consider (on an 8-Bit char system):
unsigned char a = 200;

unsigned char b = 100;

unsigned char c = ( a + b );
I'd call that over-flow, ie. you want it to be 300, but the storage system
can't accomodate it.

So it takes the result mod 256. That's 44.

Let's try swap the values of a and b without a temp variable:
int main()
{
unsigned char a = 200;
unsigned char b = 100;
a = a + b; // a == 300, b == 100
b = a - b; // a == 300, b == 200
a = a - b; // a == 100, b == 200
}
That's what'll happen it the beautiful land of jelly, where nobody gets sick
and everyone stays young for ever.

Here's what'll really happen:

int main()
{
unsigned char a = 200;
unsigned char b = 100;
a = a + b // a == 45, b == 100
Bzzzt. Out by 1. Overflow, so subtract 256. a == 44, b == 100
b = a - b // a == 200, b == 100
What? a doesn't change. b underflows, so add 256. a = 44, b == 200
a = a - b // a == 100, b == 100
a underflows, so add 256. a == 100, b == 200
}
Modulo 256, the two "programs" are identical.

Now, as far as I know, if you switch

a == 200, b == 100
you don't get:

a == 100, b == 100
But that's just human observation.

My observation would be "when in hole, stop digging."

--
Richard Herring
Nov 14 '05 #66
In article <WB************ *****@news.indi go.ie>, JKop <NU**@NULL.NULL > wrote:
Ioannis Vranos posted:
Unsigned integer types have a well defined behaviour both in C and C++.
If the variable gets increased past the maximum value then it wraps
around (begins from 0 again).

SNIP
Here's what'll really happen:

int main()
{
unsigned char a = 200;

unsigned char b = 100;

a = a + b // a == 45, b == 100

b = a - b // a == 200, b == 100

a = a - b // a == 100, b == 100

}

You need to check your math. You have
made a mistake. Here is what really happens...

bash-2.05b\$ cat demo.c
#include <stdio.h>

int main()
{
unsigned char a = 200;
unsigned char b = 100;

printf("1. %u %u\n", a, b);
a = a + b;
printf("2. %u %u\n", a, b);
b = a - b;
printf("3. %u %u\n", a, b);
a = a - b;
printf("4. %u %u\n", a, b);

return 0;
}

bash-2.05b\$ make demo
cc -O -pipe -march=pentium3 demo.c -o demo
bash-2.05b\$ ./demo
1. 200 100
2. 44 100
3. 44 200
4. 100 200
bash-2.05b\$
Nov 14 '05 #67
JKop a écrit :
Here's what'll really happen:

int main()
{
unsigned char a = 200;

unsigned char b = 100;

a = a + b // a == 45, b == 100
should be:
a = a + b; // a == 44, b == 100
b = a - b // a == 200, b == 100
should be:
b = a - b; // a == 44, b == 200
a = a - b // a == 100, b == 100
should be:
a = a - b; // a == 100, b == 200 }
Now, as far as I know, if you switch

a == 200, b == 100
you don't get:

a == 100, b == 100
No, you get a == 100, b == 200.

But that's just human observation.

^^^^^

hmmm... next time use a computer.

--
Richard
Nov 14 '05 #68
John Cochran posted:
1. 200 100
2. 44 100
3. 44 200
4. 100 200

I see! I thought that yous weren't paying attention to over/under-flow.
Nobody mentioned it at all, so I just pressumed. So is this non-undefined
behaviour?

-JKop
Nov 14 '05 #69
In article <9K************ *****@news.indi go.ie>, JKop <NU**@NULL.NULL > wrote:
John Cochran posted:
1. 200 100
2. 44 100
3. 44 200
4. 100 200

I see! I thought that yous weren't paying attention to over/under-flow.
Nobody mentioned it at all, so I just pressumed. So is this non-undefined
behaviour?

-JKop

Overflow and underflow for unsigned integers is very well defined in C and C++,
that's what people have been attempting to tell you in this thread for quite
some time.

Nov 14 '05 #70

This thread has been closed and replies have been disabled. Please start a new discussion.