473,785 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
please Help
Wiating for reply.
Nov 14 '05
271 20307
"Jerry Coffin" <jc*****@taeus. com> wrote in message
news:b2******** *************** **@posting.goog le.com...
Almost anything you'd normally do with iteration can also be done with
tail recursion.
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.
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.


Are these methods faster than x*7 on modern processors?
Q6 C/C++ : Write a function in different ways that will return f(7) =
4 and f(4) = 7 If you want to get clever with boolean values, you could try:

int f(int x) {
return (x==7*4)+(x==4* 7);
}
Huh?
I'm sure there are more variations as well.


Probably something with mod or %.
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.


The sort method is O(N*lg(N)) + O(N) if we use comparison sort. But doing
it in place without changing the order seems to be an O(N^2) algorithm, with
the outer loop i running from [0, N) and the inner loop j running from [0,
i). (Seems most people run the inner loop from [i+1, N) and then they run
into problems of how to detect if you've not seen the element already.)
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
address is already in the set (there's a loop).

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
know the answer up-front.


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.
Nov 14 '05 #11
Siemel Naran <Si*********@re move.att.net> scribbled the following
on comp.lang.c:
"Jerry Coffin" <jc*****@taeus. com> wrote in message
news:b2******** *************** **@posting.goog le.com...
Almost anything you'd normally do with iteration can also be done with
tail recursion.
What is "tail" recursion? Are there other types of recursion?
Tail recursion is first doing the computation, then recursing. Head
recursion is the other way around.
Any the question says not to use loops. But to me, recursion is a loop,
just expressed differently.
They're clearly different concepts. At least in C, recursion has a
separate local scope for all levels, while looping reuses the same
local scope for all iterations.
> Q6 C/C++ : Write a function in different ways that will return f(7) =
> 4 and f(4) = 7
If you want to get clever with boolean values, you could try:

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

Huh?


In C, the == operator returns 1 if the operands match or 0 if they
don't. Go from there.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The day Microsoft makes something that doesn't suck is probably the day they
start making vacuum cleaners."
- Ernst Jan Plugge
Nov 14 '05 #12
"Siemel Naran" <Si*********@RE MOVE.att.net> wrote:
"Jerry Coffin" <jc*****@taeus. com> wrote in message
news:b2******* *************** ***@posting.goo gle.com...

<snip>
> Q5 C/C++ : Multiply x by 7 without using multiplication (*) operator.
<snip> If you want to get clever with boolean values, you could try:

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


Huh?


I'm pretty sure Jerry meant to write:

return (x==7)*4 + (x==4)*7;

Regards
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #13
Joona I Palaste <pa*****@cc.hel sinki.fi> wrote:
Siemel Naran <Si*********@re move.att.net> scribbled the following
on comp.lang.c:
"Jerry Coffin" <jc*****@taeus. com> wrote in message
news:b2******** *************** **@posting.goog le.com...
> Q6 C/C++ : Write a function in different ways that will return f(7) =
> 4 and f(4) = 7 If you want to get clever with boolean values, you could try:

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

Huh?


In C, the == operator returns 1 if the operands match or 0 if they
don't. Go from there.


Now f returns 2 if x equals 28, or 0 otherwise. Great solution. ;-)

Regards
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #14
Irrwahn Grausewitz <ir*******@free net.de> scribbled the following
on comp.lang.c:
"Siemel Naran" <Si*********@RE MOVE.att.net> wrote:
"Jerry Coffin" <jc*****@taeus. com> wrote in message
news:b2****** *************** ****@posting.go ogle.com... <snip>
> Q5 C/C++ : Multiply x by 7 without using multiplication (*) operator. <snip> If you want to get clever with boolean values, you could try:

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


Huh?

I'm pretty sure Jerry meant to write: return (x==7)*4 + (x==4)*7;


Dang! I didn't spot that in my original reply. Jerry's original code is
equivalent to return (x==28)+(x==28) , which will return 2 if x==28 but
0 if not.

--
/-- 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."
- MAD Magazine
Nov 14 '05 #15
Irrwahn Grausewitz <ir*******@free net.de> scribbled the following
on comp.lang.c:
Joona I Palaste <pa*****@cc.hel sinki.fi> wrote:
Siemel Naran <Si*********@re move.att.net> scribbled the following
on comp.lang.c:
"Jerry Coffin" <jc*****@taeus. com> wrote in message
news:b2******** *************** **@posting.goog le.com...
> Q6 C/C++ : Write a function in different ways that will return f(7) =
> 4 and f(4) = 7
If you want to get clever with boolean values, you could try:

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

Huh?


In C, the == operator returns 1 if the operands match or 0 if they
don't. Go from there.

Now f returns 2 if x equals 28, or 0 otherwise. Great solution. ;-)


Yes, I noticed it myself later, as you can see. In my defense, it was
Jerry who wrote the incorrect code, I just failed to correct it... =)

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"And according to Occam's Toothbrush, we only need to optimise the most frequent
instructions."
- Teemu Kerola
Nov 14 '05 #16
js*******@sanch arnet.in (Jatinder) wrote in message news:<22******* *************** ****@posting.go ogle.com>...
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
please Help
Wiating for reply.


Q3 & Q12

int main()
{
int a = 10;
int b = 20;

/* Swap here */

a ^= b;
b ^= a;
a ^= b;

/* Show value code */
/* .........
* .........
*/
return 0;
}

Please correct me, if I'm wrong. :-)
Nov 14 '05 #17
boa
Jerry Coffin wrote:
js*******@sanch arnet.in (Jatinder) wrote in message news:<22******* *************** ****@posting.go ogle.com>... {snip]

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


[snip]

I'm sure there are more variations as well.

int f(int x)
{
return 11 - x;
}

boa
[snip]
Nov 14 '05 #18
Jatinder wrote:
Q3 C/C++ : Exchange two numbers without using a temporary variable.

Isn't the bitwise solution safe only for unsigned integrals?


Regards,

Ioannis Vranos
Nov 14 '05 #19
Siemel Naran wrote:
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.


The OP cross posted both in clc++ and clc, and the set of questions
obviously consider C as a subset of C++, a dangerous thing to do but anyway.
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".


Regards,

Ioannis Vranos
Nov 14 '05 #20

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

Similar topics

270
3990
by: Jatinder | last post by:
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.
12
8484
by: G. | last post by:
Hi all, During my degree, BEng (Hons) Electronics and Communications Engineering, we did C programming every year, but I never kept it up, as I had no interest and didn't see the point. But now I really want to get back into it as I see a point with GNU/Linux. I want to get my old skills back and write something or help on some projects etc. I need some good books. I used to have one called "A Book On C", but sold it,
11
2221
by: John Salerno | last post by:
Similar to the Python Challenge, does anyone know of any other websites or books that have programming puzzles to solve? I found a book called "Puzzles for Hackers", but it seems like it might be a little advanced for me, and I've also read that it focuses too much on encryption and security issues and doesn't really have coding problems, exactly. But something like that book would be fun to have.
1
13108
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the other....how i can fix this the program look like this import java.util.ArrayList; import java.util.Random;
0
2027
by: xavier vazquez | last post by:
have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the other....how i can fix this this run the random words for the program import javax.swing.JOptionPane; import java.util.ArrayList; import java.util.Random; public class CrossWordPuzzleTester {
44
3574
by: Jon Harrop | last post by:
Microsoft Research are developing a functional programming language called F# for .NET and I've been playing with it recently. I've uploaded some demos here: http://www.ffconsultancy.com/dotnet/fsharp/ I'm keen to see what Windows developers think of this language as we're considering using it to develop commercial applications on the Windows platform.
5
4473
by: ashish0799 | last post by:
HI I M ASHISH I WANT ALGORYTHMUS OF THIS PROBLEM Jigsaw puzzles. You would have solved many in your childhood and many people still like it in their old ages also. Now what you have got to do is to solve jigsaw puzzles using the computer. The jigsaw puzzle here is a square of dimension d (a puzzle with d^2 pieces) and the jigsaw pieces (all same dimensions) are of dimensions H x W (Which means the pieces have ‘H’ rows of ‘W’...
3
3209
by: oncue01 | last post by:
Word Puzzle Task You are going to search M words in an N × N puzzle. The words may have been placed in one of the four directions as from (i) left to right (E), (ii) right to left (W), (iii) up to bottom (S), or (iv) bottom to up (N). The program will print the starting place and the direction of each word. Limitations The number of words to be searched can be at most 100, the size of the puzzle N can be minimum 5 maximum 20....
4
19997
by: honey777 | last post by:
Problem: 15 Puzzle This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is an example of the puzzle: The goal is to get the tiles in order, 1 through 15, from left to right, top to bottom, by just sliding tiles into the empty square. In this configuration, the goal would be to get the 14 and 15 to switch places, without affecting any of the other squares. Your...
13
3123
by: btkuhn | last post by:
Hi guys, I'm learning Python by teaching myself, and after going through several tutorials I feel like I've learned the basics. Since I'm not taking a class or anything, I've been doing challenges/programs to reinforce the material and improve my skills. I started out with stuff like "Guess my number" games, hangman, etc. and moved on to making poker and card games to work with classes. For GUIs I created games like minesweeper, and a...
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10162
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.