Sei sulla pagina 1di 3

Design and Analysis of Algorithms

Practice Questions

1. Let A be array of positive integers of size n. Let B be an array of size n such that
B[i] =

A[1] A[2] . . . A[n]


for i = 1, 2, . . . , n
A[i]

. That is, ith element in array B is product of all the elements of A except A[i]. Give O(n)
time algorithm to fill entries of array B. Important restriction is: you are not allowed to
use division operation.
2. Let A be sorted array of integers of size n. On input integer s, give an efficient algorithm
to check whether there are elements a, b A such that s = a + b. What is time complexity
of your algorithm? Can you give O(n)-time algorithm for the problem.
3. We call a 2D array doubly sorted if elements in every row and every column of A
are in sorted order. I.e. for i = 1 to n we have A[i, 1] < A[i, 2] < . . . < A[i, n] and
A[1, i] < A[2, i] < . . . < A[n, i]. Give O(n)-time algorithm to search an element in a
doubly sorted array A (Naive algorithm takes O(n log n) time, just perform binary search
in each row separately).
4. (challenging) A teacher says: Im thinking of two natural numbers greater than 1. Try
to guess what they are. The first student knows their product and the other one knows
their sum. Following conversation happens
First: I do not know the sum.
Second: I knew that. The sum is less than 14.
First: I knew that. However, now I know the numbers.
Second: And so do I.
What were the numbers?
5. In a city, The police has surrounded the Bank. There are 50 people in the building.
Each person is either an engineer or a manager of the bank. All computer files have
been deleted, and all documents have been shredded by the managers. The problem
confronting the police is to separate the people into these two classes, so that all the
managers are locked in a room and all the engineers are freed. every people knows the
status of all others. The interrogation consists entirely of asking person i if person j is
an engineer or a manager. The engineers always tell the truth. What makes it hard is
that the managers may not tell the truth. In fact, the managers are evil geniuses who are
conspiring to confuse the interrogators.
(a) Under the assumption that more than half of the people are engineers, can you find
a strategy for the Police to find one engineer with at most 49 questions?
(b) Is this possible in any number of questions if half the people are managers?
(c) Once an engineer is found, he/she can classify everybody else. Is there a way to
classify everybody in fewer questions?
Practice Questions-1

6. In a far away land, it was known that if you drank poison, the only way to save yourself
is to drink a stronger poison, which neutralizes the weaker poison. The king that ruled
the land wanted to make sure that he possessed the strongest poison in the kingdom, in
order to ensure his survival, in any situation. So the king called the kingdoms pharmacist
and the kingdoms treasurer, he gave each a week to make the strongest poison. Then,
each would drink the other ones poison, then his own, and the one that will survive, will
be the one that had the stronger poison. The pharmacist went straight to work, but the
treasurer knew he had no chance, for the pharmacist was much more experienced in this
field, so instead, he made up a plan to survive and make sure the pharmacist dies. On
the last day the pharmacist suddenly realized that the treasurer would know he had no
chance, so he must have a plan. After a little thought, the pharmacist realized what the
treasurers plan must be, and he concocted a counter plan, to make sure he survives and
the treasurer dies. When the time came, the king summoned both of them. They drank
the poisons as planned, and the treasurer died, the pharmacist survived, and the king
didnt get what he wanted. What exactly happened there?
7. Give implement for a stack data-structure with additional support for operation FindMax.
At any point of time FindMax should return maximum among all the element present in
the stack. FindMax should work in O(1) time.
8. Give implementation for min-heap data-structure with additional support for operation
FindMedian. At any point of time FindMedian should return median of all the elements
present in the heap. FindMedian should work in O(1) time.
9. Suppose you have a collection of n coins and a balance. One of the coin is fake. Weight
of fake coin is different from other proper coins.
(a) If fake coin is heavier, how many weighings are necessary to figure out the fake coin.
(b) If you dont know whether the fake coin is lighter or heavier, how many weinings are
required?
10. Give an algorithm to enumerate the following
all permutations of n elements.
all subsets of {1, 2, 3, . . . , n} of size r.
all derengements of n elements.
11. Suppose you are given pre-order and post-order traversal of a binary tree, can you construct a unique binary tree from this input? If yes, give the algorithm. If No, why?
12. How will you implement ndimensional array? Give details on indexing mechanism.
13. Is the operation of squaring a n digit number asymptotically faster than multiplication
of two n-digit numbers?
14. Let f (n) denotes index of the winner for the Josephus problem with n people (People
numbered from 1 to n and person 1 holds the sword).

Practice Questions-2

(a) Obtain recurrence relation relating f (2n) with f (n) and f (2n + 1) with f (n).
(b) Use the recurrences obtained to design an efficient algorithm to compute f (n). What
is the time and space complexity of your algorithm?
(c) Let (ak ak1 . . . a1 a0 )2 is binary equivalent of n with ak = 1. Show that decimal
equivalent of the binary number (ak1 ak2 . . . a1 a0 ak )2 is equal to f (n).

Practice Questions-3

Potrebbero piacerti anche