473,838 Members | 1,637 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 20376
In <40************ ***@nospam.com> Julie <ju***@nospam.c om> writes:
In <cb**********@o ravannahka.hels inki.fi> Joona I Palaste <pa*****@cc.hel sinki.fi> writes:
>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.

Since you seem to be unable to understand plain English:

#include <stdio.h>

void swap(int *p, int *q) { ... }

int main()
{
int i = 10;
swap(&i, &i);
printf("%d\n", i);
return 0;
}

Try this code for different implementations of the swap function, using
a temp var and using in-place swapping. Compare the results.

This example is trivial, but the situation can realistically arise in more
complex algorithms.

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

"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 using >pointers 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

Yes, but the two variables are pointers, and they do not share the same memory
location -- they may *point* to the same location.

So, I still haven't seen two variables that share the same memory location. I
think that you can probably do it w/ placement new (C++ only!), but using (C++)
references or pointers, you can't have two variables that share the same memory
location.
Nov 14 '05 #82

"Julie" <ju***@nospam.c om> wrote in message
news:40******** *******@nospam. com...
Yes, but the two variables are pointers, and they do not share the same memory location -- they may *point* to the same location.

So, I still haven't seen two variables that share the same memory location. I think that you can probably do it w/ placement new (C++ only!), but using (C++) references or pointers, you can't have two variables that share the same memory location.

Geez, give me a break! What I've shown exhibits *exactly* the kind of
problem that can happen when trying to swap two integers using the xor
technique. Just because someone used terminology that suggested the two
variables themselves had the same memory location, surely you knew what was
meant! The problem is when both memory locations are the same, which can
happen if using pointers or references. That's all that was meant, not that
there were two *different* variables occupying the *same* memory.
-Howard

Nov 14 '05 #83
In comp.lang.c Joona I Palaste <pa*****@cc.hel sinki.fi> wrote:
job. That's all there was to it. No tests, no panel hearings, just a few
hours of talk.

I bet they saw your postings to comp.lang.c ;)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #84
Dan Pop wrote:

In <40************ ***@nospam.com> Julie <ju***@nospam.c om> writes:
In <cb**********@o ravannahka.hels inki.fi> Joona I Palaste <pa*****@cc.hel sinki.fi> writes:

>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.

Since you seem to be unable to understand plain English:

#include <stdio.h>

void swap(int *p, int *q) { ... }

int main()
{
int i = 10;
swap(&i, &i);
printf("%d\n", i);
return 0;
}

Try this code for different implementations of the swap function, using
a temp var and using in-place swapping. Compare the results.

This example is trivial, but the situation can realistically arise in more
complex algorithms.

No, I do not understand your version of plain English.

Just because you have two pointers that _point_ to the same address, this
doesn't mean that they (the variables here which are still the **pointers**)
share the same memory location.

Your definition of swap doesn't take to variables, it takes two addresses.
Even in your example in main, you are passing in the same pointer.

Show me a case of two separate variables that share the same memory location,
without using placement new. You can even use your version of 'plain English'.

Here is the point in my version of 'plain English': you can't have two
variables that share the same memory address (excluding placement new). So, if
the precondition for swap is that it operates on two variables, then it will
always work provided the precondition is met.
Nov 14 '05 #85
On 28 Jun 2004, Dan Pop wrote:
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 ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^ [snip]

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

^^^^^^^^^^^^^^^ ^^^

Tak-Shing

Nov 14 '05 #86

"Julie" <ju***@nospam.c om> wrote in message
news:40******** *******@nospam. com...
Here is the point in my version of 'plain English': you can't have two
variables that share the same memory address (excluding placement new). So, if the precondition for swap is that it operates on two variables, then it will always work provided the precondition is met.

So who ever sid the procondition was that you were swapping two non-pointer,
non-reference variables? If you're writing the swap as a function, you have
to use references or pointers, or else you'll only be swapping local copies.
That's what makes this an important consideration, because those pointer or
references parameters could be referring to the same memory location. The
swap function need to contain a check to handle that specific case.

-Howard

Nov 14 '05 #87
Julie wrote:
How can you _not_ remove an element from an array?

Here is a trivial case:

Since you use the malloc() family and this is cross posted to clc, I
assume you use C.

size_t count = 2;
int * array = (int *)malloc(sizeof (int) * count);

In C this casting is not needed.
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array , sizeof(int) * (--count));

In C this casting is not needed.

Regards,

Ioannis Vranos
Nov 14 '05 #88
On Mon, 28 Jun 2004, Tak-Shing Chan wrote:
On 28 Jun 2004, Dan Pop wrote:
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 ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^ [snip]

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

^^^^^^^^^^^^^^^ ^^^

To comp.lang.c++: sorry I didn't notice the cross-post at
the beginning. You can safely ignore my out-of-context reply.
What my post really amounts to is a (rather subtle) critique of
Dan Pop's lack of clarity in the replied-to post. [It took me
three to four readings to get his logic!]

Tak-Shing

Nov 14 '05 #89
On Mon, 28 Jun 2004, Christopher Benson-Manica wrote:
In comp.lang.c Joona I Palaste <pa*****@cc.hel sinki.fi> wrote:
job. That's all there was to it. No tests, no panel hearings, just a few
hours of talk.

I bet they saw your postings to comp.lang.c ;)

I thought he is a Java programmer? ;-)

Tak-Shing

Nov 14 '05 #90

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