473,854 Members | 1,731 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 20410
Julie wrote:
Please describe (in code) a situation where two variables share the same memory
location.

union
{
int x;
int y;
} a;

&a.x == &a.y

-josh

Nov 14 '05 #91
Tak-Shing Chan <es***@city.ac. uk> scribbled the following:
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? ;-)

I am, but I know some C as well, and my C skills *have* come to some use
in this job. I had to translate a few hundred lines of C code, written
by another company, to Java.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"You will be given the plague."
- Montgomery Burns
Nov 14 '05 #92
Dan Pop wrote:

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

However in C++98 I did not find anything else than this:
5.12 Bitwise exclusive OR operator

exclusive-or-expression:
and-expression
exclusive-or-expression ^ and-expression

The usual arithmetic conversions are performed; the result is the
bitwise exclusive OR function of the operands. The operator applies only
to integral or enumeration operands."

Regards,

Ioannis Vranos
Nov 14 '05 #93
Dan Pop wrote:
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.

You are right about that, however in reality a decent swap
implementation would check if the passed arguments have the same value
so as to avoid unneeded operations.

Regards,

Ioannis Vranos
Nov 14 '05 #94
Christopher Benson-Manica <at***@nospam.c yberspace.org> scribbled the following:
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 ;)

This interview was for my current job, which is actually my second one.
After the dinner with the CEO and the chief programmer, I gave them the
e-mail address of my previous company's chief programmer. I don't know
if they ever mailed him but if they did, I figure he said I was your
average Java programmer.
In my *first* job interview, the CEO asked if I had any code samples to
show. (He was one of the founders of the company and knew quite a bit of
programming himself.) I later e-mailed him a few university and free-
time exercise programs. Later, when he called me for another meeting,
he said that my programs were pretty trivial. But somehow I got the job
anyway.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"You can pick your friends, you can pick your nose, but you can't pick your
relatives."
Nov 14 '05 #95
Dan Pop wrote:
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.

You are right about that, however in reality a decent swap
implementation would check if the passed arguments have the same value
so as to avoid unneeded operations.

Regards,

Ioannis Vranos
Nov 14 '05 #96
Dan Pop wrote:
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.

You are right about that, however in reality a decent swap
implementation would check if the passed arguments have the same value
so as to avoid unneeded operations.

Regards,

Ioannis Vranos
Nov 14 '05 #97
josh wrote:
Julie wrote:
Please describe (in code) a situation where two variables share the
same memory
location.

union
{
int x;
int y;
} a;

&a.x == &a.y

That was good :-)

Regards,

Ioannis Vranos
Nov 14 '05 #98

"Ioannis Vranos" <iv*@guesswh.at .grad.com> wrote in message
news:cb******** **@ulysses.noc. ntua.gr...
You are right about that, however in reality a decent swap
implementation would check if the passed arguments have the same value
so as to avoid unneeded operations.

And the need for that check is exactly one of the arguments for why this
choice of implementation was no better than using a temporary variable in
the first place. If you can afford the space for the code to check this
condition, then you can surely afford the space for the temporary integer

-Howard
Nov 14 '05 #99
Howard wrote:
"Ioannis Vranos" <iv*@guesswh.at .grad.com> wrote in message
news:cb******** **@ulysses.noc. ntua.gr...

You are right about that, however in reality a decent swap
implementatio n would check if the passed arguments have the same value
so as to avoid unneeded operations.

And the need for that check is exactly one of the arguments for why this
choice of implementation was no better than using a temporary variable in
the first place. If you can afford the space for the code to check this
condition, then you can surely afford the space for the temporary integer

I think the non-temporary requirement is not for space concerns but to
find out how c00l we are. :-) Even under severe space concerns there is
always space for a *temporary* variable. If there are space concerns to
the extreme, then we should write numbers in its memory directly. :-)

Regards,

Ioannis Vranos
Nov 14 '05 #100

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