473,854 Members | 1,879 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 20406
JKop 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?

For *unsigned integers*, the behaviour is well-defined both for overflow
and underflow (it wraps around). So you can get the maximum value of an
unsigned integer by using -1. This one is used *extensively*.

For example:
// Equivalent to unsigned long li=numeric_limi ts<unsigned long>::max();
unsigned long li=-1;
unsigned char uc=-1;

and so on.

Regards,

Ioannis Vranos
Nov 14 '05 #71
In <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.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #72
In <cb**********@o ravannahka.hels inki.fi> Joona I Palaste <pa*****@cc.hel sinki.fi> writes:
JKop <NU**@null.null > scribbled the following
on comp.lang.c:
Am I the only person here that thinks it's complete bullshit to think you
can swap the values of two variables without a temporary variable?! It
simply cannot be done. Why? Consider this, you have two containers, each of
capacity 3 litres. Each of them is filled with 2 litres of water. Swap the
water from the containers. Okay... let's just poor all of one of them into
the other. Mammy mammy! It was an accident, I didn't realize you can't put 4
litres of water into a 3 litre container.
Fools.

The only foolish assumption is that bits behave like water...
It *can* be done! Not with all kinds of variables, but with unsigned
integer types, it's easy.
If you can do it with unsigned integer types, you can do it with all kinds
of objects, by aliasing them with arrays of unsigned char.
Not one, but *two* ways to do it have been
shown in this thread. Of course it will break down if those variables
happen to share the same memory location, which can be the case if using
pointers and indirecting through them.

OTOH, if you're using pointers, there's nothing preventing you from
testing the pointers for equality, before starting, is there?

So, the method can be universally used in C, if there is a *good* reason.
And the only good reason I can imagine is the failure to allocate memory
for a temporary object (some objects can be greater than others, some
systems can have smaller memories than others...).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #73
In <cb**********@u lysses.noc.ntua .gr> Ioannis Vranos <iv*@guesswh.at .grad.com> writes:
Joona I Palaste wrote:
It *can* be done! Not with all kinds of variables, but with unsigned
integer types,

I had somewhere in my mind the unsigned restriction, but then I checked
the standard and saw that XOR is safe to be applied on both integral
types and enumerations. Then how does this unsigned restriction come?

4 Some operators (the unary operator ~, and the binary operators
<<, >>, &, ^, and |, collectively described as bitwise
operators) are required to have operands that have integer
type. These operators return values that depend on the internal
representations of integers, and have implementation-defined
and undefined aspects for signed types.

Consider, for example, 0 ^ 0, whose result (an int with all bits set) is
a trap representation, as explicitly allowed by C99 for one's complement.

It's hard to find an example for two's complement using the ^ operator,
but it can be done with ~INT_MAX, if the representation with the sign
bit set and all the value bits reset is a trap representation (again,
explicitly allowed by C99 for two's complement and sign-magnitude).

Whether real world implementations have such trap representations is a
completely different story...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #74
Dan Pop wrote:

In <cb**********@o ravannahka.hels inki.fi> Joona I Palaste <pa*****@cc.hel sinki.fi> writes:
JKop <NU**@null.null > scribbled the following
on comp.lang.c:
Am I the only person here that thinks it's complete bullshit to think you
can swap the values of two variables without a temporary variable?! It
simply cannot be done. Why? Consider this, you have two containers, each of
capacity 3 litres. Each of them is filled with 2 litres of water. Swap the
water from the containers. Okay... let's just poor all of one of them into
the other. Mammy mammy! It was an accident, I didn't realize you can't put 4
litres of water into a 3 litre container.
Fools.

The only foolish assumption is that bits behave like water...
It *can* be done! Not with all kinds of variables, but with unsigned
integer types, it's easy.

If you can do it with unsigned integer types, you can do it with all kinds
of objects, by aliasing them with arrays of unsigned char.
Not one, but *two* ways to do it have been
shown in this thread. Of course it will break down if those variables
happen to share the same memory location, which can be the case if using
pointers and indirecting through them.

Please describe (in code) a situation where two variables share the same memory
location.

OTOH, if you're using pointers, there's nothing preventing you from
testing the pointers for equality, before starting, is there?

So, the method can be universally used in C, if there is a *good* reason.
And the only good reason I can imagine is the failure to allocate memory
for a temporary object (some objects can be greater than others, some
systems can have smaller memories than others...).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de

Nov 14 '05 #75
Jerry Coffin wrote:
Q7 Remove duplicates in array

You can't really "remove" an element from an array, so this is poorly
defined. If it was a C++ vector (for example) std::sort and
std::unique would render it trivial, as would inserting the elements
into an std::set, and then copying them back out. Doing it quickly
while retaining the original order is a little more challenging.

How can you _not_ remove an element from an array?

Here is a trivial case:

size_t count = 2;
int * array = (int *)malloc(sizeof (int) * count);
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array , sizeof(int) * (--count));

Are you saying that an element hasn't been removed?
Nov 14 '05 #76

"Julie" <ju***@nospam.c om> wrote in message
news:40******** *******@nospam. com...
Jerry Coffin wrote:
Q7 Remove duplicates in array

You can't really "remove" an element from an array, so this is poorly
defined. If it was a C++ vector (for example) std::sort and
std::unique would render it trivial, as would inserting the elements
into an std::set, and then copying them back out. Doing it quickly
while retaining the original order is a little more challenging.

How can you _not_ remove an element from an array?

Here is a trivial case:

size_t count = 2;
int * array = (int *)malloc(sizeof (int) * count);
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array , sizeof(int) * (--count));

Are you saying that an element hasn't been removed?

Well, that's not *really* removing an element from the array, it's simply
reallocating data storage. How would your example look if you wanted to
remove an element from other than the last position? You'd have to write
code to shift the other elements to the front of the array, not just resize
the memory allocation.

-Howard

Nov 14 '05 #77
Howard wrote:

"Julie" <ju***@nospam.c om> wrote in message
news:40******** *******@nospam. com...
Jerry Coffin wrote:
> Q7 Remove duplicates in array

You can't really "remove" an element from an array, so this is poorly
defined. If it was a C++ vector (for example) std::sort and
std::unique would render it trivial, as would inserting the elements
into an std::set, and then copying them back out. Doing it quickly
while retaining the original order is a little more challenging.
How can you _not_ remove an element from an array?

Here is a trivial case:

size_t count = 2;
int * array = (int *)malloc(sizeof (int) * count);
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array , sizeof(int) * (--count));

Are you saying that an element hasn't been removed?

Well, that's not *really* removing an element from the array, it's simply
reallocating data storage.

Well, if that isn't removing, then I don't know what is... Reallocation is
merely an implementation detail, net result is that an element has been
removed.
How would your example look if you wanted to
remove an element from other than the last position? You'd have to write
code to shift the other elements to the front of the array, not just resize
the memory allocation.

Exactly. I started w/ the trivial case of removing the last element. Removing
any other position would merely involve calling memmove or similar to shift the
contents down and then reallocating.

Still sounds like removing an element from an array to me...
Nov 14 '05 #78

"Julie" <ju***@nospam.c om> wrote in message
news:40******** *******@nospam. com...
Not one, but *two* ways to do it have been
shown in this thread. Of course it will break down if those variables
happen to share the same memory location, which can be the case if usingpointers and indirecting through them.

Please describe (in code) a situation where two variables share the same

memory location.

Just like described, using pointers. (References can also be used.)

void pswap( int* px, int* py )
{
*px = *px ^ ^py;
*py = *py ^ *px;
*px = *px ^ *py;
}

....calling code:...
int x = 3;
int* px = &x;
int* py = &x;
pswap( px, py );
-Howard
Nov 14 '05 #79
js*******@sanch arnet.in (Jatinder) wrote in message news:<22******* *************** ****@posting.go ogle.com>...
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.

#include <cstdio>
using namespace std;int main(){const char* s="#include <cstdio>%cusi ng
namespace std;int main(){const char*
s=%c%s%c;printf (s,10,34,s,34,1 0);}%c";printf( s,10,34,s,34,10 );}

Greetings, Bane.
Nov 14 '05 #80

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