I
Ioannis Vranos
Ioannis said:Isn't the bitwise solution safe only for unsigned integrals?
I just checked the standard, it is safe for both integral and
enumeration types.
Regards,
Ioannis Vranos
Ioannis said:Isn't the bitwise solution safe only for unsigned integrals?
Ioannis said:However in both cases bitwise operations are guaranteed to be safe only
on unsigned integrals, so the above had better include the remark:
"where x is of unsigned integral type".
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.
Siemel Naran said:What is "tail" recursion? Are there other types of recursion?
Any the question says not to use loops. But to me, recursion is a loop,
just expressed differently.
Are these methods faster than x*7 on modern processors?
Huh?
Probably something with mod or %.
There's another. Have two iterators, first one pointing to first element,
the second pointing to the second. The second one is the fast iterator and
you increment it twice in each iteration. The first iterator is the slow
iterator and you increment it once in each iteration. If the list is not
circular the fast iterator will hit NULL at some point. If the list is
circular the fast iterator will equal to the slow iterator at some point.
Siemel said:That might be too out of the box .
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.
Q2 Write a C++ program without using any loop (if, for, while etc) to
print numbers from 1 to 100 and 100 to 1;
I found these questions on a web site and wish to share with all of u
out there,Can SomeOne Solve these Porgramming puzzles.
Use a universal-character name to denote the semicolon, or use #error,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.
#include<iostream>Q2 Write a C++ program without using any loop (if, for, while etc) to
print numbers from 1 to 100 and 100 to 1;
Assuming that a and b are ints and ((&a)!=(&b))Q3 C/C++ : Exchange two numbers without using a temporary variable.
Keep generating powers of 2 till one equals (Ans: yes) or exceedsQ4 C/C++ : Find if the given number is a power of 2.
The expression f(7)=4 calls f with the argument 7, assigns 4 to theQ5 C/C++ : Multiply x by 7 without using multiplication (*) operator. x+x+x+x+x+x+x
Q6 C/C++ : Write a function in different ways that will return f(7) =
4 and f(4) = 7
Array elements cannot be "removed".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.
If by "convert (integer) number in binary" you mean to output theQ12 Swap two numbers without using a third variable. See the ans to Q3
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
please Help
Wiating for reply.
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.
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.
Joona said:JKop <[email protected]> scribbled the following
It *can* be done! Not with all kinds of variables, but with unsigned
integer types, it's easy. 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.
foo += bar;
bar = foo - bar;
foo = foo - bar;
The "xor" method works on all unsigned integer types,
and [OT] if you program in assembler, it works on
any two memory locations or registers unless you
cause a trap along the way. [/OT]
Joona said:Nick Landsberg <[email protected]> scribbled the following
Joona said:JKop <[email protected]> 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.
It *can* be done! Not with all kinds of variables, but with unsigned
integer types, it's easy. 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.
The "xor" method works on all unsigned integer types,
and [OT] if you program in assembler, it works on
any two memory locations or registers unless you
cause a trap along the way. [/OT]
The "xor" method will *not* work on two variables with the same memory
location. You'd expect the swap to be a no-op, but it ends up setting
both variables to 0, because it "xors" the first variable with itself,
yielding 0, and then keeps "xorring" this 0 with itself.
Wayne Rasmussen said:A job interview is for finding a match. The company is also under the
microscope. Would you really want to work for a company who gives this
as the big programming test?
boa said:Jerry said:(e-mail address removed) (Jatinder) wrote in message{snip]
[snip]Q6 C/C++ : Write a function in different ways that will return f(7) =
4 and f(4) = 7
I'm sure there are more variations as well.
int f(int x)
{
return 11 - x;
}
I just "interviewed" with a company in Los Angeles (that little country
between Mexico and USA). They didn't even talk to me at all. They handed me
a "personality test", which also included an intelligence test (and I've
been through Mensa testing - it was hard), and sat me in a room for 2 hours.
At the end they took the answer sheet and said they'd get back to me.
I passed the test, I guess, because I get to go back next week for another 2
hour programming test. If I pass that, I get to move to round three which is
to face a panel of their programmers in a room for questioning. I'm guessing
it'll be about 2 hours...
Whee...
Foobarius Frobinium said:(e-mail address removed) (Jatinder) wrote in message
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??
Nick Landsberg said:You are correct. I should have said two different/distinct
memory locations.
Prateek R Karandikar said:(e-mail address removed) (Jatinder) wrote in message
Keep generating powers of 2 till one equals (Ans: yes) or exceeds
(Ans: no) the given number.
Mabden said:return !(x || 0x0001);
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.