473,854 Members | 1,967 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 20406
Julie wrote:
Howard wrote:

Haven't we beaten this dead horse long enough???

-Howard


No, just getting started.

Those that are not interested in the outcome or further discussion of this
topic/thread, are welcome to ignore the thread in their newsreader. In
Netscape (4.x), press 'K' and you will not be bothered any more w/ our
senseless blather on swapping and the (dis)similarity of variable
addresses.


Hi,
may I offer a tentative definition for the term "variable" that hopefully
describes the way I use the term: To me a variable is a textual
representation of an lvalue within a piece of source code. The lvalue
condition distinguishes variables from arbitrary expressions. A variable
can change its value, and the basic means of accomplishing that is the
assignment. For instance, I consider "*p" a variable whenever "p" deontes a
pointer to something non-const.

It is clear from the definition I gave that there can be literally
thousands of variables that refer to the same memory location.

What is your prefered definition of variable?
Best

Kai-Uwe Bux
Nov 14 '05 #181
In <cb***********@ ns.felk.cvut.cz > ld <do*****@contro l.felk.cvut.cz> writes:
Ioannis Vranos wrote:
Ioannis Vranos wrote:
Jatinder wrote:

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

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


I just checked the standard, it is safe for both integral and
enumeration types.

...but it definitely is a nonsense for floating point numbers.
Therefore, there's no general way how to "Exchange two numbers without
using a temporary variable."


Wrong! The values of two objects of *any* type can be exchanged on
a byte by byte basis, without using a temporary variable. Is that
general enough for you?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #182
In <40************ ***@nospam.com> Julie <ju***@nospam.c om> writes:
Dan Pop wrote:

In <40************ ***@nospam.com> Julie <ju***@nospam.c om> writes:
>Dingo wrote:
>>
>> Julie <ju***@nospam.c om> wrote in message news:<40******* ********@nospam .com>...
>> > Gordon Burditt wrote:
>> > >
>> > > >> >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.
>> > >
>> > > A union?
>> >
>> > Nope -- a union is still a single variable, with just different ways to access
>> > it.
>> Could you please provide your preferred definition of "variable".
>> I wish to understand why you think a union member is not one.
>
>I'll retract my statement -- I'll agree that a union does allow for two
>variables to share the same memory address.
And, if you think even harder, you'll realise that it's the same thing
with pointers: *p and *q are two different variables, but they may share
the same memory address, depending on how the p and q variables have been
initialised (e.g. one may be pointing to one member of the union, the
other may be pointing to the other member of the union mentioned in the
union example :-)


Negative.


I see: I was pushing your thinking capabilities too hard ;-)
I do not consider pointers or references that point to the same
variable to *be* the same variable. In your example, yes _p_ and _q_ are
variables, _*p_ and _*q_ are not, they merely (may) point to 0 or more
variables.


Wrongo! *p and *q don't point to anything (unless p and q are pointers to
pointers), they are as "variables" as you can get. It is p and q that
point to variables.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #183
Julie <ju***@nospam.c om> wrote:
pete wrote:

Julie wrote:
> Please describe (in code) a situation where two
> variables share the same memory location.


In post
http://groups.google.com/groups?selm...mindspring.com
in the function n_sort, the variable (*node),
has the same address as either (tail) or (head) after this line:

node = GT(head, tail) ? &tail : &head;


Try again.

No *variable* is sharing the same address. Node, head, and tail all have
different addresses, unless you can say that any of the following is true:

&node == &head == &tail

It doesn't matter what node _points_ to, it matters where node _is_.

Under your logic, the following two variables:

int a = 1;
int b = 2;

become the _same_ same variable with the following:

b = 1;

Surely you don't now consider a and b the same _variable_, do you?

Remember, we are talking about _addresses_ of variables, not the _value_ of
variables.


<my two cents>

1. Technically, the discussion is about objects, identifiers that
designate/denote objects (aka variable names in C++), and values.

2. Julie is right, insofar as no two distinct identifiers can
designate the same object - with the obvious exception of union
members, but consider: union { int a, b; } u, *up = &u;
Now u.a, u.b, up->a and up->b denote the same object, but they are
*not* identifiers that denote objects, but expressions consisting
of two operands and one operator each. The member designators a
and b cannot be used in their own right to denote an object, they
can only appear as second operand of the . or -> operator.

3. The others are right, in that two pointer objects can reference the
same memory location (= have the same value, just like two ints
can hold equal values). This however has no bearing on the "swap a
variable with itself" issue: swapping the pointer values won't work
portably with XOR anyway, and using indirection to access the
pointed-to object involves an expression that is *not* a variable.

4. The infamous "swap with XOR" method has not only limitations WRT
the type of the operands[*], it's also broken by design, because
it fails to correctly swap the value of an object (sic!) with
itself.

5. I suggest to drop the term "variable" all together, *especially* in
cross-posts between c.l.c and c.l.c++, since it's not defined at
all in the C standard and IMHO somewhat vaguely defined in the C++
standard.

[*] Modulo accessing the representation of each operand byte-per-byte
through a pointers to unsigned char.

</my two (actually five ;-) cents>

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 #184
Dan Pop wrote:

In <40************ ***@nospam.com> Julie <ju***@nospam.c om> writes:
Dan Pop wrote:

In <40************ ***@nospam.com> Julie <ju***@nospam.c om> writes:

>Dingo wrote:
>>
>> Julie <ju***@nospam.c om> wrote in message news:<40******* ********@nospam .com>...
>> > Gordon Burditt wrote:
>> > >
>> > > >> >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.
>> > >
>> > > A union?
>> >
>> > Nope -- a union is still a single variable, with just different ways to access
>> > it.
>> Could you please provide your preferred definition of "variable".
>> I wish to understand why you think a union member is not one.
>
>I'll retract my statement -- I'll agree that a union does allow for two
>variables to share the same memory address.

And, if you think even harder, you'll realise that it's the same thing
with pointers: *p and *q are two different variables, but they may share
the same memory address, depending on how the p and q variables have been
initialised (e.g. one may be pointing to one member of the union, the
other may be pointing to the other member of the union mentioned in the
union example :-)


Negative.


I see: I was pushing your thinking capabilities too hard ;-)
I do not consider pointers or references that point to the same
variable to *be* the same variable. In your example, yes _p_ and _q_ are
variables, _*p_ and _*q_ are not, they merely (may) point to 0 or more
variables.


Wrongo! *p and *q don't point to anything (unless p and q are pointers to
pointers), they are as "variables" as you can get. It is p and q that
point to variables.


We disagree.
Nov 14 '05 #185
>> > > >Please describe (in code) a situation where two variables share
the same memory
> > >location.
> >
> > A union?
>
> Nope -- a union is still a single variable, with just different waysto access > it.

Could you please provide your preferred definition of "variable".
I wish to understand why you think a union member is not one.


I'll retract my statement -- I'll agree that a union does allow for two
variables to share the same memory address.


Could you please provide your definition of "variable"?

I'll provide mine: modifiable lvalue.

By this definition, these are variables:

foo.a (where foo is a struct or union)
foo[1] (where foo is an array of dimension at least 2)
foo[j] (where foo is an array, and j is in range)
foo[k] (where foo is an array, and k is in range)

AND, I'll claim that foo[j] and foo[k] are variables, and they
are usually different variables (occupy different memory), but
they could refer to the same memory if j == k.

Gordon L. Burditt
Nov 14 '05 #186
Ioannis Vranos <iv*@guesswh.at .grad.com> wrote in message news:<cb******* ****@ulysses.no c.ntua.gr>...
int main (void)
{
if (printf("Hello World")) {}

/* Only needed for C90 compliance */
if (exit(EXIT_SUCC ESS),1){}
}


Here was my less elegant solution. But I didn't even use anything that
looks like a ';'. (commas look a lot like semicolons...) :)

#include <stdio.h>
#include <stdlib.h>

int main() {
while(printf("h ello\n") && 0) {
}
while(((int (*)(int))(exit) )(0) || 1) {}
}
Nov 14 '05 #187
"xarax" <xa***@email.co m> wrote in message news:<cv******* ***********@new sread2.news.pas .earthlink.net> ...
Those BS interviews asking about how to twiddle bits are a
waste of time. If I can't recall an exact algorithm, I just
look it up in one of my many reference books, including the
3 volumes of Knuth. The overall approach to solving a problem
and how I think about designing a solution is what's really
important. Don't sweat the small things, that's what junior
programmers are for.


When an interviewer uses these types of questions correctly they are
trying to see how you think. They aren't looking for the exact (or
even a correct) answer or exact syntax, but that you understand the
fundamentals the question is based on and that you can reason through
problems. Getting the right answer isn't the point.

Unfortunately these types of questions are often abused by people who
don't know how to interview. And if they don't ask anything but
bit-twiddler questions that's also a problem.
Nov 14 '05 #188
In article <jk************ *************** *****@4ax.com> ir*******@freen et.de writes:
Julie <ju***@nospam.c om> wrote: ....
No *variable* is sharing the same address. Node, head, and tail all have
different addresses, unless you can say that any of the following is true:

.... 1. Technically, the discussion is about objects, identifiers that
designate/denote objects (aka variable names in C++), and values.


Technically the case hinges on the definition of "variable", which C does
not give. Let Julie show the implementation of a swap function of two
"variables" in C, and after that we can look further.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 14 '05 #189
"Dik T. Winter" wrote:

In article <jk************ *************** *****@4ax.com> ir*******@freen et.de writes:
> Julie <ju***@nospam.c om> wrote:

...
> >No *variable* is sharing the same address. Node, head, and tail all have
> >different addresses, unless you can say that any of the following is true:

...
> 1. Technically, the discussion is about objects, identifiers that
> designate/denote objects (aka variable names in C++), and values.


Technically the case hinges on the definition of "variable", which C does
not give. Let Julie show the implementation of a swap function of two
"variables" in C, and after that we can look further.


Actually, the original says "two numbers" -- it disintegrated into 'show me two
variables that have the same memory location' soon afterward.

It has been shown that two variables can have have the same memory location
using unions. Therefore, in order to successfully use the XOR swap trick, the
precondition needs to be: uses two variables that do not share the same
address.

-end
Nov 14 '05 #190

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
8493
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
2232
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
13123
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
2032
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
3588
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
4477
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
3213
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
20019
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
3133
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
11031
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10763
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10371
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
7918
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
7082
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
5744
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4563
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
4159
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3188
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.