# 2011 Microsoft Placement Paper

#### Description

_________

MICROSOFT PLACEMENT PAPERS NISHNA 3/14/2011

Microsoft Placement papers 2005 -’11

MICROSOFT PLACEMENT PAPERS 2011 Microsoft Placement Paper 1. 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). 2. 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). 3. Given 3 lines of assembly code : find it is doing. IT was to find absolute value. 4. If you are on a boat and you throw out a suitcase, Will the level of water increase. 5. Print an integer using only putchar. Try doing it without using extra storage. 6. Write C code for (a) deleting an element from a linked list (b) traversing a linked list 7. Compute the number of ones in an unsigned integer. ANS. #define count_ones(x) \ (x=(0xaaaaaaaa&x)>>1+(0x55555555&x), \ x=(0xcccccccc&x)>>2+(0x33333333&x), \ x=(0xf0f0f0f0&x)>>4+(0x0f0f0f0f&x), \ x=(0xff00ff00&x)>>8+(0x00ff00ff&x), \ x=x>>16+(0x0000ffff&x)) 8. Compute the discrete log of an unsigned integer. ANS. #define discrete_log(h) \ (h=(h>>1)|(h>>2), \ h|=(h>>2), \ h|=(h>>4), \ h|=(h>>8), \ h|=(h>>16), \ h=(0xaaaaaaaa&h)>>1+(0x55555555&h), \ h=(0xcccccccc&h)>>2+(0x33333333&h), \ h=(0xf0f0f0f0&h)>>4+(0x0f0f0f0f&h), \ h=(0xff00ff00&h)>>8+(0x00ff00ff&h), \ h=(h>>16)+(0x0000ffff&h))

9. Let f(k) = y where k is the y-th number in the increasing sequence of non-negative integers with the same number of ones in its binary representation as y, e.g. f(0) = 1, f(1) = 1, f(2) = 2, f(3) = 1, f(4) = 3, f(5) = 2, f(6) = 3 and so on. Given k >= 0, compute f(k). 10. A character set has 1 and 2 byte characters. One byte characters have 0 as the first bit. You just keep accumulating the characters in a buffer. Suppose at some point the user types a backspace, how can you remove the character efficiently. (Note: You cant store the last character typed because the user can type in arbitrarily many backspaces) 11. Write a function to find the depth of a binary tree. 12. Given two strings S1 and S2. Delete from S2 all those characters which occur in S1 also and finally create a clean S2 with the relevant characters deleted. 13. Assuming that locks are the only reason due to which deadlocks can occur in a system. What would be a foolproof method of avoiding deadlocks in the system.

14. Given an array t[100] which contains numbers between 1..99. Return the duplicated value. Try both O(n) and O(nsquare). 15. Given an array of characters. How would you reverse it. ? How would you reverse it without using indexing in the array. 16. 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 1)|(h>>2), \ h|=(h>>2), \ h|=(h>>4), \ h|=(h>>8), \ h|=(h>>16)) 17. Let f(k) = y where k is the y-th number in the increasing sequence of non-negative integers with the same number of ones in its binary representation as y, e.g. f(0) = 1, f(1) = 1, f(2) = 2, f(3) = 1, f(4) = 3, f(5) = 2, f(6) = 3 and so on. Given k >= 0, compute f(k). 18. A character set has 1 and 2 byte characters. One byte characters have 0 as the first bit. You just keep accumulating the characters in a buffer. Suppose at some point the user types a backspace, how can you remove the character efficiently. (Note: You cant store the last character typed because the user can type in arbitrarily many backspaces) 19. Reverse the bits of an unsigned integer. ANS. #define reverse(x) \ (x=x>>16|(0x0000ffff&x)8|(0x00ff00ff&x)4|(0x0f0f0f0f&x)2|(0x33333333&x)1|(0x55555555&x)1+(0x55555555&x), \ x=(0xcccccccc&x)>>2+(0x33333333&x), \ x=(0xf0f0f0f0&x)>>4+(0x0f0f0f0f&x), \ x=(0xff00ff00&x)>>8+(0x00ff00ff&x), \ x=x>>16+(0x0000ffff&x))

2010 Microsoft Placement Paper 1. A version of the "There are three persons X Y Z, one of which always lies".. etc.. 2. There are 3 ants at 3 corners of a triangle, they randomly start moving towards another corner.. what is the probability that they don't collide. 3. Write an efficient algorithm and C code to shuffle a pack of cards.. this one was a feedback process until we came up with one with no extra storage. 4. The if (x == 0) y = 0 etc.. 5. Some more bit wise optimization at assembly level 6. 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). 7. 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). 8. Given 3 lines of assembly code : find it is doing. IT was to find absolute value. 9. If you are on a boat and you throw out a suitcase, Will the level of water increase. 10. Print an integer using only putchar. Try doing it without using extra storage. 11. Let f(k) = y where k is the y-th number in the increasing sequence of non-negative integers with the same number of ones in its binary representation as y, e.g. f(0) = 1, f(1) = 1, f(2) = 2, f(3) = 1, f(4) = 3, f(5) = 2, f(6) = 3 and so on. Given k >= 0, compute f(k).

12. A character set has 1 and 2 byte characters. One byte characters have 0 as the first bit. You just keep accumulating the characters in a buffer. Suppose at some point the user types a backspace, how can you remove the character efficiently. (Note: You cant store the last character typed because the user can type in arbitrarily many backspaces) 13. What is the simples way to check if the sum of two unsigned integers has resulted in an overflow. 14. How do you represent an n-ary tree? Write a program to print the nodes of such a tree in breadth first order. 15. Give a very good method to count the number of ones in a "n" (e.g. 32) bit number. ANS. Given below are simple solutions, find a solution that does it in log (n) steps. Iterative function iterativecount (unsigned int n) begin int count=0; while (n) begin count += n & 0x1 ; n >>= 1; end return count; end Sparse Count function sparsecount (unsigned int n) begin int count=0; while (n) begin count++; n &= (n-1); end return count ; end 16. Given a rectangular (cuboidal for the puritans) cake with a rectangular piece removed (any size or orientation), how would you cut the remainder of the cake into two equal halves with one straight cut of a knife ? 17. You're given an array containing both positive and negative integers and required to find the sub-array with the largest sum (O(N) a la KBL). Write a routine in C for the above. 18. Given an array of size N in which every number is between 1 and N, determine if there are any duplicates in it. You are allowed to destroy the array if you like. [ I ended up giving about 4 or 5 different solutions for this, each supposedly better than the others ]. 19. Write code for reversing a linked list. 20. Write, efficient code for extracting unique elements from a sorted list of array. e.g. (1, 1, 3, 3, 3, 5, 5, 5, 9, 9, 9, 9) -> (1, 3, 5, 9).

Microsoft IT campus placement test at Amrita School of Engineering, Coimbatore on 24th January 2011 1.)Written Test -90 mins 2.) 2-3 Rounds of Interview Written round was approx 60 marks. 3 coding questions ..one was a simple recursion based , a 10 marker was about conversion from decimal to binary and third was a simple string manipulation question. Lot of situation based questions. ->How would you test a remote controlled car..write test cases for that. ->How would you handle a situation where your tv goes blank while watching a match. ->what would be your long term and short term solution for flights which run out of fuel in air due to lack of clearance to land durring foggy winter mornings. ->How would you safeguard your system where you hav lot of confidential data in it.

2 code snippets in c/c++ 2 MCQ questions - concepts of DB

OLD MICROSOFT QUESTIONS Question 1. [10] Find the output of the following program segment #include char *c[]={"ENTNG", "NST","AMAZI","FIRBE"}; char** cp[]={c+3, c+2, c+1, c}; char ***cpp= cp; void main() { printf("%s",**++cpp); printf("%s ",*--*++cpp+3); printf("%s",*cpp[-2]+3); printf("%s",cpp[-1][-1]+1); } Question 2. [5] Write a function delete(struct node** Head) to delete the linked list node by node by deallocating them from the memory and at the end assign the head pointer to NULL. void deleteListTest(struct node* headRef) { struct node* myList=Listonetwothree(); delete(headRef); } //write your code here Question 3. [10] Write a function Compute(int x) such that it prints the values of x, 2x, 4x, 8x ... till the value doesn't exceed 20000. After reaching 20000, it again comes back from ..8x, 4x, 2x, x and stops there. Note: (1) You can't use any local variables in the function (2) You can't use any loops (for or while or do..while) or any GOTO statement. Question 4. [10] List all data structures you would use for a memory management module. Question 5. [5] Share 2 high complexity and 2 low complexity test cases for a coke vending (ATM) machine. Question 6. [10] Explain 3 high priority test cases for the performance of MSN search engine. Answers: Two of the trickiest questions and the easiest ones: (1) AMAZING BEST (3)void Compute (int x) { cout