PAPER: MICROSOFT PLACEMENT PAPER (TECHNICAL-C, OS)
1. An array of size k contains integers between 1 and n. You are given an additional scratch array of size n. Compress the original array by removing duplicates in it. What if k << n?
ANS. Can be done in O(k) time i.e. without initializing the auxiliary array!
2. An array of integers. The sum of the array is known not to overflow an integer. Compute the sum. What if we know that integers are in 2's complement form?
ANS. If numbers are in 2's complement, an ordinary looking loop like for(i=total=0;i< n;total+=array[i++]); will do. No need to check for overflows!
3. An array of characters. Reverse the order of words in it.
ANS. Write a routine to reverse a character array. Now call it for the given array and for each word in it.
4. An array of integers of size n. Generate a random permutation of the array, given a function rand_n() that returns an integer between 1 and n, both inclusive, with equal probability. What is the expected time of your algorithm?
ANS. "Expected time" should ring a bell. To compute a random permutation, use the standard algorithm of scanning array from n downto 1, swapping i-th element with a uniformly random element <= i-th. To compute a uniformly random integer between 1 and k (k < n), call rand_n() repeatedly until it returns a value in the desired range.
5. An array of pointers to (very long) strings. Find pointers to the (lexicographically) smallest and largest strings.
ANS. Scan array in pairs. Remember largest-so-far and smallest-so-far. Compare the larger of the two strings in the current pair with largest-so-far to update it. And the smaller of the current pair with the smallest-so-far to update it. For a total of <= 3n/2 strcmp() calls. That's also the lower bound.
6. Write a program to remove duplicates from a sorted array.
ANS. int remove_duplicates(int * p, int size)
int current, insert = 1;
for (current=1; current < size; current++)
if (p[current] != p[insert-1])
p[insert] = p[current];
7. C++ ( what is virtual function ? what happens if an error occurs in constructor or destructor. Discussion on error handling, templates, unique features of C++. What is different in C++, ( compare with unix).
8. Given a list of numbers ( fixed list) Now given any other list, how can you efficiently find out if there is any element in the second list that is an element of the first list (fixed list).
9. Given 3 lines of assembly code : find it is doing. IT was to find absolute value.
10. If you are on a boat and you throw out a suitcase, Will the level of water increase.
11. Print an integer using only putchar. Try doing it without using extra storage.
12. Write C code for (a) deleting an element from a linked list (b) traversing a linked list
13. What are various problems unique to distributed databases
14. Declare a void pointer ANS. void *ptr;
15. Make the pointer aligned to a 4 byte boundary in a efficient manner ANS. Assign the pointer to a long number and the number with 11...1100 add 4 to the number
16. What is a far pointer (in DOS)
17. What is a balanced tree
18. Given a linked list with the following property node2 is left child of node1, if node2 < node1 else, it is the right child.
How do you convert the above linked list to the form without disturbing the property. Write C code for that.
O ? O ?
determine where do A and C go
19. Describe the file system layout in the UNIX OS
ANS. describe boot block, super block, inodes and data layout
20. In UNIX, are the files allocated contiguous blocks of data
ANS. no, they might be fragmented
21.How is the fragmented data kept track of Describe the direct blocks and indirect blocks in UNIX file system
22. Write an efficient C code for 'tr' program. 'tr' has two command line arguments. They both are strings of same length. tr reads an input file, replaces each character in the first string with the corresponding character in the second string. eg. 'tr abc xyz' replaces all 'a's by 'x's, 'b's by 'y's and so on. ANS.
a) have an array of length 26.
put 'x' in array element corr to 'a'
put 'y' in array element corr to 'b'
put 'z' in array element corr to 'c'
put 'd' in array element corr to 'd'
put 'e' in array element corr to 'e'
and so on.
c = getc();
putc(array[c - 'a']);
23. what is disk interleaving
24. why is disk interleaving adopted
25. given a new disk, how do you determine which interleaving is the best a) give 1000 read operations with each kind of interleaving determine the best interleaving from the statistics
26. draw the graph with performance on one axis and 'n' on another, where 'n' in the 'n' in n-way disk interleaving. (a tricky question, should be answered carefully