Programming puzzles – 2

1. Write an efficient C program to count the number of bits set in an unsigned integer.
i/p o/p

==== ===

0(00) 01(01)

12(10) 13(11)

2….. …
2. Write a small C program, which while compiling takes another programfrom input terminal, and on running gives the result for the secondprogram. (NOTE: The key is, think UNIX).

Suppose, the program is 1.c
Then, while compiling
$ cc -o 1 1.c
int main()
{
printf(“Hello Worldn”);
}
^D
$ ./1
Hello World
$

3. Given a string s1 and a string s2, write a snippet to say whether s2 is arotation of s1 using only one call to strstr routine?(eg given s1 = ABCD and s2 = CDAB, return true, given s1 = ABCD, and s2 = ACBD , return false)

4. What’s the “condition” so that the following codesnippet prints both HelloWorld !

if “condition” printf (“Hello”);
else
printf(“World”);

5. Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, … shows the first 11 ugly numbers. By convention, 1 is included.
Write a program to find and print the 1500’th ugly number.

6. Write a C program which when compiled and run, prints out a messageindicating whether the compiler that it is compiled with, allows /* */comments to nest.

7. Write a C function that will print 1 to N one per each line on the stdoutwhere N is a int parameter to the function. The function should not use while, for, do-while loops, goto statement, recursion, and switchstatement.

8. int main()
{
int i, n = 20;
for (i = 0; i < n; i–)
printf(“*”);
return 0;
}

Change/add only one character and print ‘*’ exactly 20 times.
(there are atleast 3 solutions to this problem 🙂

9. You are given have a datatype, say X in C. The requirement is to get the size of the datatype, without declaring a variable or a pointer variable of that type,
And, of course without using sizeof operator !

10. Given a singly-linked, find out the mid point of a single linked list in a single parse of the list. Assume the program would be loaded in read-only memory so no manipulation of the list is allowed.

11. You are given a circular single linked list of sufficiently many number of nodes(say more than 1 crore). You need to delete a node say P and you are given a pointer to P in the circular single list.

Suggest the most efficient methodology of deleting the node P from the
circular single linked list without rounding about the circular single
linked list.

12. Write a C Program to reverse a stack “in place” using recursion ?
You can only use the following ADT functions on Stack:
IsEmpty
IsFull
Push
Pop
Top

13. You are provided with two stacks, and pop() and push() functions for them. You have to implement queue i.e. enqueue() and dequeue() using the available operations.

14. How do you reverse the words in a string?

“My name is Amit Agarwal”
to
“Agarwal Amit is name My”

A O(n) and ‘in space’ solution is appreciable.

15. You are given a sequence of numbers from 1 to n-1 with one of the numbers repeating only once. (example: 1 2 3 3 4 5).

* How can you find the repeating number?
* What if i give you the constraint that you can’t use a dynamic amount
of memory (i.e. the amount of memory you use can’t be related to n)?
* What if there are two repeating numbers,instead of one (and the same
memory constraint?)

16. Given an array of numbers, except for one number all the others, occur twice. Give an algorithm to find that number which occurs only once in the array.

17. There is a series of numbers in ascending order. All these numbers have the same number of binary 1s in them. Given the number of 1 bits set in the numbers, write an algorithm/C program to find the nth number in the series.

18. Given a stack S, write a C program to sort the stack (in the ascending order). We are not allowed to make any assumptions about how the stack is implemented. The only functions to be used are:
Push
Pop
Top
IsEmpty
IsFull

19. Given a linked list, your are asked to find out the nth last element in the linked-list. (n would be given as the argument to the function)

20. The numbers are represented in linked-list with each node representing a digit of the number as follows: 123 == 1 2 3 NULL 999 == 9 9 9 NULL

Write a C program to add two numbers.

I/P : 9 9 9 NULL
1 NULL

O/P : 1 0 0 0 NULL

You can assume that the number of elements in the linked-list is available
to you.
struct List
{
Node* head;
int noe; /* number of elements */
};

21. There is a 100-story building and you are given two eggs. The eggs (and the building) have an interesting property that if you throw the egg from a floor number less than X, it will not break. And it will always brake if the floor number is equal or greater than X.

Assuming that you can reuse the eggs which didn’t broke, you got to find
X in a minimal number of throws.

Give an algorithm to find X in minimal number of throws.

22. You are given a singly link-list such that each node of this list is also a head of another link list of the same type. So, how does one flatten the linked-list
struct node {
void *data; /* could be anything */
struct node *next;
struct node *down;
};

23. Given: 2 Unsorted single linked list Todo: Fastest and optimised way to merge and make a single sorted list with these 2 unsorted single linked list.

24. Given a numbers x and n, where n is a power of 2, write C code, which gives the multiple of n which is greater than or equal to x. ex : I/P: 13 8
O/P: 16

I/P: 17 16
O/P: 32

The challenge: Do not use division or modulo operator.

25.
In C, copying of array as follows is not possible:

int a[10],b[10];
a = b;
a = GetAnArrayOfTenElements();

Can you think of a simple hack, which would enable us to get this effect?

Leave a Reply

Your email address will not be published. Required fields are marked *