Anda di halaman 1dari 6

INTERVIEW PROGRAMMING QUESTION

Question 1. There is a big file of words which is dynamically changing. We are continuously adding some words into it. How would you keep track of top 10 trending words at each moment? Question 2. Write code for minHeapify() operation. Question 3. Design a data structure for the following operations: I. Enqueue II. Dequeue III. Delete a given number(if it is present in the queue, else do nothing) IV. isNumberPresent All these operations should take O(1) time. Question 4. Write a function that returns the length of the longest leaf-to-leaf path in a binary tree. Question 5. You are given an array of positive integers. Convert it to a sorted array with minimum cost (minimum number of operations). Only valid operation are 1) Decrement -> cost = 1 2) Delete an element completely from the array -> cost = value of element For example: 4,3,5,6, -> cost 1 //cost is 1 to make 4->3 10,3,11,12 -> cost // cost 3 to remove 3 Question 6. Given an array of integers, find Pythagorean triplets. i.e. find a,b and c which satisfies a^2 + b^2 = c^2. Integers could be positive or negative. Question 7. Given an array all of whose elements are positive numbers, find the maximum sum of a subsequence with the constraint that no 2 numbers in the sequence should be adjacent in the array. So 3 2 7 10 should return 13 (sum of 3 and 10) or 3 2 5 10 7 should return 15 (sum of 3, 5 and 7) Question 8. There are n petrol bunks arranged in circle. Each bunk is separated from the rest by a certain distance. You choose some mode of travel which needs 1litre of petrol to cover 1km distance. You cant infinitely draw any amount of petrol from each bunk as each bunk has some limited petrol only. But you know that the sum of litres of petrol in all the bunks is equal to the distance to be covered. ie let P1, P2, Pn be n bunks arranged circularly. d1 is distance between p1 and p2, d2 is distance between p2 and p3. dn is distance between pn and p1.Now find out the bunk from where the travel can be started such that your mode of travel never runs out of fuel. Question 9. A tree is represented in a matrix form such that A[i,j] = 1 if j is the ancestor of i. Otherwise A[i,j] = 0. Given this construct a normal binary search tree.

KAPIL SINGH RAWAT

DIET RISHIKESH

Page 1

INTERVIEW PROGRAMMING QUESTION

Question 10. A 2D array is such that each row in it is in ascending order and each column is in ascending order. How will you search for an element in this array? Question 11. A city is represented as a grid where each line represents a road. As it is a grid, all the horizontal roads intersect with all vertical roads. A bus may enter the city in the left-bottom junction with a North seeing face (direction of the bus). You can move to any of the intersections with a proper control string. Eg LLMMR L, R, M representes that the bus has to be turned to its left, right and then has to move one step forward and reach the next intersection. How will you design this scenario? What classes are needed? What member variables and functions are needed? Question 12. Java has an inbuilt hashmap class. It usually takes object id as a reference to hash the key values. Instead how could you make it take the state of the object (if both the objects have same set of values for all the member variables, they are in the same state) to hash the key? hashmap.hash(new foo(), xyz); hashmap.hash(new foo(), xyz); Both of them should hash into the same. How will you do that? Question 13. There is a binary tree of size N. All nodes are numbered between 1-N(inclusive). There is a N*N integer matrix Arr[N][N], all elements are initialized to zero. So for all the nodes A and B, put Arr[A][B] = 1 if A is an ancestor of B (NOT just the immediate ancestor). Question 14. There is a N*N integer matrix Arr[N][N]. From the row r and column c, we can go to any of the following three indices: I. Arr[ r+1 ][ c-1 ] (valid only if c-1>=0) II. Arr[ r+1 ][ c ] III. Arr[ r+1 ][ c+1 ] (valid only if c+1<=N-1) So if we start at any column index on row 0, what is the largest sum of any of the paths till row N-1. Question 15. Two robots land with their parachutes on an infinite one-dimensional number line. They both release their parachutes as soon as they land and start moving. They are allowed only to make use of the following functions. I. moveLeft() // robot moves to left by 1 unit in 1 unit time II. moveRight() // robot moves to right by 1 unit in 1 unit time III. noOperation() // robot does not move and takes 1 unit time IV. onTopOfParachute() // returns true if the robot is standing on top of either of the parachute, else false V. didWeMeet() // returns true if the robot meets to the other robot, else false

KAPIL SINGH RAWAT

DIET RISHIKESH

Page 2

INTERVIEW PROGRAMMING QUESTION


Write a function in order to make the robots meet each other. Robots will be executing the same copy of this function. Question 16. Given a linked list containing character in each node, segregate its nodes in such a way that all nodes containing a vowel are moved to the end of the linked list. We will have to maintain the order. Question 17. You are given a linked list and a parameter k. You will have to swap values in a certain fashion, swap value of node 1 with node k, then node (k+1) with node 2k and go on doing this in the similar fashion. Question 18. You are given many slabs each with a length and a breadth. A slab i can be put on slab j if both dimensions of i are less than that of j. In this similar manner, you can keep on putting slabs on each other. Find the maximum stack possible which you can create out of the given slabs. Question 19. The above question is raised to 3 dimensions. ii) The above question was then raised to k dimensions. iii) Then there were many questions asked on compilers and dynamic memory allocation. Question20. You are given pairs of numbers. In a pair the first number is smaller with respect to the second number. Suppose you have two sets (a, b) and (c, d), the second set can follow the first set if b<c.So you can form a long chain in the similar fashion. Find the longest chain which can be formed. Question 21. You are given a linked list and an integer k. Reverse every consecutive k nodes of the given linked list. Question 22. Given a 2D array containing only 0/1s and each row is in sorted order. Find the row which contains maximum number of 1s. Question 23. Design an object oriented parking lot. What classes and functions will it have? It should say, full, empty and also be able to find spot for Valet parking. The lot has 3 different types of parking: regular, handicapped and compact. Question 24. Given three linked lists,where each linked list represents a number, add the three lists and return the resultant list. 5->1->2->NULL 9->1->NULL 7->2->2->NULL Output :: 1->3->2->5->NULL Question 25. Given an array consisting of both positive and negative numbers, 0 is considered as positive, rearrange the elements such that positive and negative numbers are placed alternatively, constraints are that it should be in-place and order of elements should not change.

KAPIL SINGH RAWAT

DIET RISHIKESH

Page 3

INTERVIEW PROGRAMMING QUESTION

Question 26. Given a binary tree, no two adjacent nodes have same color, but all leaves should be in same color. You can fill only with two colors. Write a function to find whether a given tree can be colored using above scenario. Question 27. Given a class with n people,where each people plays a game with all other people. Results are with you. You have to arrange them in a queue with a condition that, a[i] should have won a[i-1], for all I, you dont need to care about a[i-2] . (a[i] may win or lose a[i-2]). Question 28. Given a matrix with 1s and 0s, Construct a matrix such that a[i][j]=1, if only every element in ith row and jth column is 1, otherwise 0. You have to use constant space and O(mn) time complexity. Question 29. Complexity?
f(i) = 2*f(i+1) + 3*f(i+2) For (int i=0; i < n; i++) F[i] = 2*f[i+1]

Question 30. Given a 2-D array of 0s and 1s, find islands in it. An Island is 1s together. E.g (below there is U shaped island) 0100001 0100001 0100001 0100001 0111111 Question 31. Given a chess board of finite length , start postion of a knight , an end position. i)find whether the end position is reachable by the knight. ii) Number of minimum hops required to reach that position. Question 32. Write a function which compress string AAACCCBBD to A3C3B2D and other function to generate from the compressed. Question 33. Given an integer array where every number appears even number of times and only one number appears odd times. Find the number. Question 34. Given a Binary Search tree along with the parent pointer, find the next largest node for the given node. Give the time and space complexity. The node Structure is
class Node { Int data; Node left; Node right; Node parent; }

KAPIL SINGH RAWAT

DIET RISHIKESH

Page 4

INTERVIEW PROGRAMMING QUESTION

Question 35. Given a string in the form of a Linked List, check whether the string is palindrome or not. Dont use extra memory. Give the time complexity. The node structure is
Class Node { Char data; Node next; }

Question 36. Write a program to list all the possible words from the given set of data in the same order. ( eg : given word : nokiamobile O/P : nokia mobile : given word : samsung O/P : 1. SAMSUNG 2.SAM SUNG(considering sam as a word) ) Question 37. Find if the binary representation of a number is palindrome. The function should work irrespective of number of bytes for an integer. Suppose if our machine is 4 bytes for an int, how will you use the program for 8 byte machine. Question 38. Given n coins for two players playing a game. Each player picks coins from the given n coins in such a way that he can pick 1 to 5 coins in one turn and the game continues for both the players. The player who picks the last coin looses the game. You have to tell that for given n coins who looses the game? Question 39. Given an array of n numbers with repetition of numbers. You need to find the max length of continuous sub array with at max 3 unique elements. For eg array: 1 2 3 1 4 3 4 1 2 ans: 6 (3 1 4 3 4 1) Question 40. Given a singly linked list, swap every 2 nodes, for odd number of input; retain the last node as it is. Eg: Input: 5 13 15 18 20 11 6 7 Output: 13 5 18 15 11 20 7 6 Question 41. Given two Binary Search Trees, merge them and create a single Balanced Binary Search Tree. Question 42. You are given an array of integers. It is sorted, but rotated. Find an better than O(n) algorithm to find an element in an array. Question 43. Given a binary tree, find a path that is equal to a given sum. Path starts from root and end at a leaf node. Question 44. Write a function that returns a node in a tree given two parameters: pointer to the root node and the inorder traversal number of the node. The only information stored in the tree is the number of children for each node.

KAPIL SINGH RAWAT

DIET RISHIKESH

Page 5

INTERVIEW PROGRAMMING QUESTION

KAPIL SINGH RAWAT ECE||DIET RISIKESH kapilrawat1508@gmail.com

KAPIL SINGH RAWAT

DIET RISHIKESH

Page 6

Anda mungkin juga menyukai