Velocity Reviews > Programming Puzzle

# Programming Puzzle

Jatinder
Guest
Posts: n/a

 06-26-2004
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

Joona I Palaste
Guest
Posts: n/a

 06-26-2004
Jatinder <(E-Mail Removed)> scribbled the following
on comp.lang.c:
> 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.

Done to death here on comp.lang.c.

> Q4 C/C++ : Find if the given number is a power of 2.

Easy with some bitwise arithmetic.

> Q5 C/C++ : Multiply x by 7 without using multiplication (*) operator.

Easy peasy. Repeated addition will do the trick.

> Q6 C/C++ : Write a function in different ways that will return f(7) =
> 4 and f(4) = 7

int f(int x) {
return x==4 ? 7 : x==7 ? 4 : 0;
}

> Q7 Remove duplicates in array

You can't remove anything from an array. You can only modify the
values of its elements.

> Q8 Finding if there is any loop inside linked list.

Should be covered in any basic data structures course.

> Q9 Remove duplicates in an no key access database without using an
> array

Impossible without access into a no key access database.

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

This one is actually a full-blown game.

> Q12 Swap two numbers without using a third variable.

And how is this any different from Q3?

> Given an array (group) of numbers write all the possible sub groups of
> this group.

Search for the definition of a "power set". The algorithm shoudln't be
too hard to figure out.

> Q14 Convert (integer) number in binary without loops.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"This isn't right. This isn't even wrong."
- Wolfgang Pauli

Christian Bau
Guest
Posts: n/a

 06-26-2004
In article <(E-Mail Removed) >,
http://www.velocityreviews.com/forums/(E-Mail Removed) (Jatinder) wrote:

> 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

Assuming that these questions are from an interview for a programming
job, I must say that most of them are incredibly useless at finding a
good programmer. Take Q3: The correct answer is: Why would anyone want
to do that? Take Q10: You know the answer or you don't. If your name is
Gödel or Turing, you might find a solution on your own, but otherwise
this just tests some very obscure knowledge. I guess these questions are
only used to check your reaction to a stressful situation (which is also
an incredibly useless way at finding a good programmer).

If I was given this list of questions, I would tell them that most of
them are pointless and then examine Q11, because it is the only
interesting one. Maybe a different response if you are desperate for a
job.

Siemel Naran
Guest
Posts: n/a

 06-26-2004
"Joona I Palaste" <(E-Mail Removed)> wrote in message news:cbkf50\$a76
> Jatinder <(E-Mail Removed)> scribbled the following

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

>
> Done to death here on comp.lang.c.

Not familiar with the first two. Q1, how? Is #define SEMICOLON ; valid?
Q2, is recursion a valid answer? Or even just writing the numbers
explicitly printf("1\n2"), etc? For Q3 one can use ^= 3 times, though I
wonder if this is faster than the usual one with a temp variable and 3
assignments on various platforms.

> > Q4 C/C++ : Find if the given number is a power of 2.

>
> Easy with some bitwise arithmetic.

x & (x-1) evaluates to zero if the number is an exact power of 2.

> > Q5 C/C++ : Multiply x by 7 without using multiplication (*) operator.

>
> Easy peasy. Repeated addition will do the trick.
>
> > Q6 C/C++ : Write a function in different ways that will return f(7) =
> > 4 and f(4) = 7

>
> int f(int x) {
> return x==4 ? 7 : x==7 ? 4 : 0;
> }

Another is realize 4 = %100 and 7 = %111 in binary, so leave the leftmost
bit on, and flip the other 2. Thus: return x ^ %11, or return x ^ 3.

> > Q7 Remove duplicates in array

>
> You can't remove anything from an array. You can only modify the
> values of its elements.

Fine, then how to replace the duplicates with NULL or the like, or move the
elements one down so that { 1, 2, 1, 1, 4, 3 } becomes { 1, 2, 4, 3,
anything, anything }?

> > Q8 Finding if there is any loop inside linked list.

>
> Should be covered in any basic data structures course.
>
> > Q9 Remove duplicates in an no key access database without using an
> > array

>
> Impossible without access into a no key access database.

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

>

Bizarre stuff!

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

>
> This one is actually a full-blown game.
>
> > Q12 Swap two numbers without using a third variable.

>
> And how is this any different from Q3?
>
> > Given an array (group) of numbers write all the possible sub groups of
> > this group.

>
> Search for the definition of a "power set". The algorithm shoudln't be
> too hard to figure out.

Someone posted something like this in the C++ newsgroup. If you have 3
numbers 1, 2, 3 then make a number of 3 bits %000. Then just add 1 until
you max out. So you get %000, %001, %010, %011, %100, %101, %110, %111. If
the bit is 0 it means that number is not in the group and if the bit it 1 it
means the number is in the group, so %001 is the group "1", and %011 is the
group "1,2", and %101 is the group "1,3". In C++ you could maybe use
std::bitset.

But I think you could do it using recursion too. So f(3,1) prints the
combinations "3,..." and f(3,0) prints combinations without the 3. The part
in ... is the combinations with 2 numbers, so f(2,1) prints "2,..." and
f(2,0) prints "...". The second ... is the combinations with 1 numbers,
just "1" and "".

> > Q14 Convert (integer) number in binary without loops.

>
> Already done here on comp.lang.c.

How, if not recursion? Maybe even lookup tables, which I used to implement
a fast lgdown(x) function which gives log to base 2 of x.

Dale Henderson
Guest
Posts: n/a

 06-26-2004
>>>>> "Jatinder" == Jatinder <(E-Mail Removed)> writes:

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

Jatinder> Programming Puzzles

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

Jatinder> Q1 Write a "Hello World" program in 'C' without using a
Jatinder> semicolon.

Jatinder> Q2 Write a C++ program without using any loop (if, for,
Jatinder> while etc) to print numbers from 1 to 100 and 100 to 1;

void countup(int i){
printf("%d\n",i);
(void) ((i<100)?countup(i+1):i);
}
countup(1);

countdown is left as an excercise

This is C. I'm reading this in comp.lang.c. C++ is offtopic

Jatinder> Q3 C/C++ : Exchange two numbers without using a
Jatinder> temporary variable.

This is a FAQ. There is no good way. (20.15c)

Jatinder> Q4 C/C++ : Find if the given number is a power of 2.

I'd like to know the answer to this one myself.

Jatinder> Q5 C/C++ : Multiply x by 7 without using multiplication
Jatinder> (*) operator.

x<<3-x;
this only works if x is unsigned.

Jatinder> Q6 C/C++ : Write a function in different ways that will
Jatinder> return f(7) = 4 and f(4) = 7

int f(int i){
return i^3;
}

Jatinder> Q7 Remove duplicates in array

Jatinder> Q8 Finding if there is any loop inside linked list.

This is similar to Q7. Your looking for duplicate pointer values.

Jatinder> Q9 Remove duplicates in an no key access database
Jatinder> without using an array

Jatinder> Q10 Write a program whose printed output is an exact
Jatinder> copy of the source. Needless to say, merely echoing the
Jatinder> actual source file is not allowed.

This is a FAQ. 20.34

Jatinder> Q11 From a 'pool' of numbers (four '1's, four '2's
Jatinder> .... four '6's), each player selects a number and adds
Jatinder> it to the total. Once a number is used, it must be
Jatinder> removed from the pool. The winner is the person whose
Jatinder> number makes the total equal 31 exactly.

Jatinder> Q12 Swap two numbers without using a third variable.
See Q3

Jatinder> Given an array (group) of numbers write all the possible
Jatinder> sub groups of this group.

Jatinder> Q14 Convert (integer) number in binary without loops.

Jatinder> Q3,12 are similar , Q7 is simple & I know there answer

I'd say Q3 and Q12 are identical.

--
Dale Henderson

"Imaginary universes are so much more beautiful than this stupidly-
constructed 'real' one..." -- G. H. Hardy

Allan Bruce
Guest
Posts: n/a

 06-27-2004

"Siemel Naran" <(E-Mail Removed)> wrote in message
news:bgnDc.32213\$(E-Mail Removed)...
> "Joona I Palaste" <(E-Mail Removed)> wrote in message

news:cbkf50\$a76
> > Jatinder <(E-Mail Removed)> scribbled the following

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

> >
> > Done to death here on comp.lang.c.

>
> Not familiar with the first two. Q1, how? Is #define SEMICOLON ; valid?

no, but consider this

int main (void)
{
if (printf("Hello World"))
{}

if (exit(EXIT_SUCCESS))
{}
}

Allan

CBFalconer
Guest
Posts: n/a

 06-27-2004
Allan Bruce wrote:
>

.... snip ...
>
> no, but consider this
>
> int main (void)
> {
> if (printf("Hello World"))
> {}
> if (exit(EXIT_SUCCESS))
> {}
> }

Illegal. No #include for prototype of variadic function, nor
EXIT_SUCCESS value, and exit is a void function. The compiler
should barf.

I am trying to construct something that revolves around:

if (printf("Hello ") - printf("World\n")) {...}

which statement could cause either "Hello World" or "WorldHello".

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.

Mabden
Guest
Posts: n/a

 06-27-2004
"Christian Bau" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> In article <(E-Mail Removed) >,
> (E-Mail Removed) (Jatinder) wrote:
>
> >
> > 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.

[snip]

>
> If I was given this list of questions, I would tell them that most of
> them are pointless and then examine Q11, because it is the only
> interesting one. Maybe a different response if you are desperate for a
> job.

Maybe you would get the job for walking out...

Siemel Naran
Guest
Posts: n/a

 06-27-2004
"Mabden" <mabden@sbc_global.net> wrote in message news:B5uDc.6258

> Maybe you would get the job for walking out...

That might be too out of the box .

Jerry Coffin
Guest
Posts: n/a

 06-27-2004
(E-Mail Removed) (Jatinder) wrote in message news:<(E-Mail Removed). com>...

[ ... ]

> Q1 Write a "Hello World" program in 'C' without using a semicolon.

One obvious method (open to a number of variations) is:

#include <stdio.h>

int main() {
if ( printf("Hello world"))
{}
}

> Q2 Write a C++ program without using any loop (if, for, while etc) to
> print numbers from 1 to 100 and 100 to 1;

Almost anything you'd normally do with iteration can also be done with
tail recursion.

> Q3 C/C++ : Exchange two numbers without using a temporary variable.

This has come up about 4 weeks into every new semester for years, with
a slightly lighter treatment on a quarterly basis.

> Q4 C/C++ : Find if the given number is a power of 2.

There are many ways. Pick N as a power of 2, and compare it to N-1
and see if something doesn't occur to you.

> Q5 C/C++ : Multiply x by 7 without using multiplication (*) operator.

One is to just add x together 7 times. Those of us who remember
writing multiplication routines for old processors that didn't have
multiply instructions can easily reduce that to (x<<2)|(x<<1)|x.
Those who've studied Booth's algorithm might try (x<<3)-x, though
without extra bits for the intermediate value, this can overflow.

> Q6 C/C++ : Write a function in different ways that will return f(7) =
> 4 and f(4) = 7

Obvious:
int f(int x) {
if ( x == 7)
return 4;
if ( x == 4)
return 7;
}
Roughly as obvious is to use switch/case intsead.

Using arrays, you can do things like:

int f(int x) {
int rets[] = { 4, 7};
return rets[x>>2];
}

Or if you want to ensure defined results for inputs other than 4 or 7:

int f(int x) {
int rets[] = {4,7};
return rets[(x>>2)&1];
}

If you want to get clever with boolean values, you could try:

int f(int x) {
return (x==7*4)+(x==4*7);
}

or:

int f(int x) {
rturn (x==7*4)|(x==4*7);
}

If you prefer strictly bit-wise manipulation, you might prefer:

int f(int x) {
return x ^ 3;
}

I'm sure there are more variations as well.

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

> Q8 Finding if there is any loop inside linked list.

One obvious way would be to create a set of pointers to nodes. Walk
the list, inserting each node's address into the set. Quit when you
reach a node with next == NULL (there's no loop) or a node whose

There's an alternative that saves memory, but basically destroys the
list if it does contain a loop: as you walk the list, modify each
'next' pointer to point at the previous node. Eventually, you'll
reach either a node with next==NULL, in which case there's no loop, or
else you'll get back to the original head of the list (in which case
there's a loop, and you've wreaked havoc on your list). If the list
doesn't contain a loop, you can re-walk it, again reversing each
pointer, to restore the original list.

Better yet, just ensure the list is constructed sanely, and you'll

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

Much like Q3, but comes up a little further into the semster/quarter.

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

If I'm figuring this correctly, it's a pretty boring game. If I go
first, I pick '1' on my first two moves, and you can't win. If I go
second, I pick '2' on my first three moves, and you can't win.

Tic-tac-toe quickly gets boring because neither player can win unless
his opponent makes a mistake. This is even worse, because the same is
true, BUT a player doesn't even have to look at what his opponent has
done to avoid a mistake. The only challenge is ensuring that you take
advantage when he does make a mistake, and (again, assuming I'm
figuring things correctly) that should be quite trivial.

> Given an array (group) of numbers write all the possible sub groups of
> this group.

Recursion is probably your friend on this one as well.

> Q14 Convert (integer) number in binary without loops.

As mentioned wrt Q2, almost any iteration is trivially expressed as
tail recursion. The wording doesn't make it clear whether this is
supposed to be a conversion FROM binary or TO binary, but either is
pretty easy to do recursively.

--
Later,
Jerry.

The universe is a figment of its own imagination.