Professional Documents
Culture Documents
Hiring Questionnaire
Hiring Questionnaire
The largest rectangle is shown in the shaded area, which has area = 10 unit.
For example,
return 10.
2)(Avg time on interviewBit : 30 mins) Problem Score
4(https://www.interviewbit.com/problems/unique-paths-in-a-grid/)
Given a grid of size m * n, lets assume you are starting at (1,1) and your goal is to reach
(m,n). At any instance, if you are on (x,y), you can either go to (x, y + 1) or (x + 1,
y).
Now consider if some obstacles are added to the grids. How many unique paths would there
be?
Example :
[
[0,0,0],
[0,1,0],
[0,0,0]
]
Each element in the array represents your maximum jump length at that position.
For example:
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
Input:
Example:
7) Score 6-7
Given a collection of candidate numbers (C) and a target number (T), find all unique
combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
Note:
All numbers (including target) will be positive integers.
Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤
ak).
The solution set must not contain duplicate combinations.
Example :
Given candidate set 10,1,2,7,6,1,5 and target 8,
A solution set is:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]
8) Problem Score
Given an array and a sum value, find all possible unique triplets in that array whose sum is
equal to the given sum value. If no such triplets can be formed from the array, then print “No
triplets can be formed”, else print all the unique triplets. For example, if the given array is {12, 3,
6, 1, 6, 9} and given sum is 24, then the unique triplets are (3, 9, 12) and (6, 6, 12) whose sum
is 24.
Examples:
Input : array = {12, 3, 6, 1, 6, 9} sum = 24
Output : [[3, 9, 12], [6, 6, 12]]
Input : array = {-2, 0, 1, 1, 2} sum = 0
Output : [[-2, 0, 2], [-2, 1, 1]]
Example 2:
Input: "cbbd"
Output: "bb"
Input: 1->2->3->4->5->6->7->8->9->10->
11->12->13->14->15->NULL,
k=4
d = -1
Output: 2->3->4->1->6->7->8->5->10->11
->12->9->14->15->13->NULL
Explanation: Here, at the end of linked
list, remaining nodes are less than k, i.e.
only three nodes are left while k is 4.
Rotate those 3 nodes also by d.
Example 1:
Input: s = "applepenapple", wordDict = ["apple", "pen"]
Output: true
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
Note that you are allowed to reuse a dictionary word.
Example 2:
Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
Output: true
13 ) Given a Binary Tree, find vertical sum of the nodes that are in same vertical line. Print all
Examples:
1
/ \
2 3
/ \ / \
4 5 6 7
Vertical-Line-3: has three nodes: 1,5,6 => vertical sum is 1+5+6 = 12
14) How to implement LRU caching scheme? What data structures should be used?
15)Check if all levels of two trees are anagrams or not
Given two binary trees, we have to check if each of their levels are anagrams of each other or
not.
Example:
Tree 1:
Level 0 : 1
Level 1 : 3, 2
Level 2 : 5, 4
Tree 2:
Level 0 : 1
Level 1 : 2, 3
Level 2 : 4, 5
16) Check if two trees are Mirror | Set 2
Given two Binary Trees, returns true if two trees are mirror of each other, else false.
Mirror Tree :
https://www.interviewbit.com/problems/largest-distance-between-nodes-of-a-tree/
17) Given n friends, each one can remain single or can be paired up with some other friend.
Each friend can be paired only once. Find out the total number of ways in which friends can
remain single or can be paired up. (DP)
Examples :
Input : n = 3
Output : 4
Explanation
{1}, {2}, {3} : all single
{1}, {2, 3} : 2 and 3 paired but 1 is single.
{1, 2}, {3} : 1 and 2 are paired but 3 is single.
{1, 3}, {2} : 1 and 3 are paired but 2 is single.
Note that {1, 2} and {2, 1} are considered same.
17)Rat in a Maze
A Maze is given as N*N binary matrix of blocks where source block is the upper left most block
i.e., maze[0][0] and destination block is lower rightmost block i.e., maze[N-1][N-1]. A rat starts
from source and has to reach the destination. The rat can move only in two directions: forward
and down.
In the maze matrix, 0 means the block is a dead end and 1 means the block can be used in the
path from source to destination. Note that this is a simple version of the typical Maze problem.
For example, a more complex version can be that the rat can move in 4 directions and a more
{1, 0, 0, 0}
{1, 1, 0, 1}
{0, 1, 0, 0}
{1, 1, 1, 1}
Following is the solution matrix (output of program) for the above input matrx.
{1, 0, 0, 0}
{1, 1, 0, 0}
{0, 1, 0, 0}
{0, 1, 1, 1}
All enteries in solution path are marked as 1.
https://www.interviewbit.com/problems/assign-mice-to-holes/
]
18) Policemen catch thieves
Examples:
1->2->3->4->NULL
4->3->2->1->NULL
1->2->3->4->5->NULL
5->4->3->2->1->NULL
Input: NULL
Output: NULL
Input: 1->NULL
Output: 1->NULL
19) Given an array and a number k where k is smaller than size of array, we need to find
the k’th smallest element in the given array. It is given that ll array elements are distinct.
Examples:
Input: arr[] = {7, 10, 4, 3, 20, 15}
k = 3
Output: 7
k = 4
Output: 10
20) Tiling Problem()
Given a “2 x n” board and tiles of size “2 x 1”, count the number of ways to tile the given
board using the 2 x 1 tiles. A tile can either be placed horizontally i.e., as a 1 x 2 tile or
vertically i.e., as 2 x 1 tile.
Examples:
Input n = 3
Output: 3
Explanation:
We need 3 tiles to tile the board of size 2 x 3.
We can tile the board using following ways
1) Place all 3 tiles vertically.
2) Place first tile vertically and remaining 2 tiles horizontally.
3) Place first 2 tiles horizontally and remaining tiles vertically
Input n = 4
Output: 5
Explanation:
For a 2 x 4 board, there are 5 ways
1) All 4 vertical
2) All 4 horizontal
3) First 2 vertical, remaining 2 horizontal
4) First 2 horizontal, remaining 2 vertical
5) Corner 2 vertical, middle 2 horizontal
21) The Celebrity Problem(DP)
In a party of N people, only one person is known to everyone. Such a person m
ay be
present in the party, if yes, (s)he doesn’t know anyone in the party. We can only ask
questions like “does A know B? “. Find the stranger (celebrity) in minimum number of
questions.
which returns t rue if A knows B, false otherwise. How can we solve the problem.
22) Sort a stack using recursion
Given a stack, sort it using recursion. Use of any loop constructs like while, for..etc is
not allowed. We can only use the following ADT functions on Stack S:
Example:
Write a function detectAndCountLoop() that checks whether a given Linked List contains
loop and if loop is present then returns count of nodes in loop. For example, loop is
present in below linked list and length of loop is 4. If loop is not present, then function
24) Add two numbers represented by linked lists | Given two numbers represented by two
linked lists, write a function that returns the sum list. The sum list is linked list
representation of the addition of two input numbers. It is not allowed to modify the lists.
Also, not allowed to use explicit extra space
Example
Input:
First List: 5->6->3 // represents number 563
Second List: 8->4->2 // represents number 842
Output
Resultant list: 1->4->0->5 // represents number 1405
Given length of wall w and shelves of two lengths m and n, find the number of each type
of shelf to be used and the remaining empty space in the optimal solution so that the
empty space is minimum. The larger of the two shelves is cheaper so it is preferred.
However cost is secondary and first priority is to minimize empty space on wall.
Examples:
Input : w = 24 m = 3 n = 5
Output : 3 3 0
We use three units of both shelves
and 0 space is left.
3 * 3 + 3 * 5 = 24
So empty space = 24 - 24 = 0
Another solution could have been 8 0 0
but since the larger shelf of length 5
is cheaper the former will be the answer.
Input : w = 29 m = 3 n = 9
Output : 0 3 2
0 * 3 + 3 * 9 = 27
29 - 27 = 2
Input : w = 24 m = 4 n = 7
Output : 6 0 0
6 * 4 + 0 * 7 = 24
24 - 24 = 0
Multiple Choice Questions:
1)Postorder traversal of a given binary search tree, T produces the following sequence of keys
10, 9, 23, 22, 27, 25, 15, 50, 95, 60, 40, 29 Which one of the following sequences of keys can
be the result of an in-order traversal of the tree T?
A)9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95
B)9, 10, 15, 22, 40, 50, 60, 95, 23, 25, 27, 29
C)29, 15, 9, 10, 25, 22, 23, 27, 40, 60, 50, 95
D) 95, 50, 60, 40, 27, 23, 22, 25, 10, 9, 15, 29
2) An array of integers of size n can be converted into a heap by adjusting the heaps rooted at
each internal node of the complete binary tree starting at the node ⌊(n – 1) /2⌋, and doing this
adjustment up to the root node (root node is at index 0) in the order ⌊(n – 1)/2⌋, ⌊(n – 3)/ 2⌋, …..,
0. The time required to construct a heap in this manner is
(A) O(log n)
(B) O(n)
(C) O (n log log n)
(D) O(n log n)
3) Which of the following sorting algorithms can be used to sort a random linked list with
minimum time complexity?
A)Insertion Sort
B)Quick Sort
C)Heap Sort
D)Merge Sort
4) What is the output of following function for start pointing to first node of following linked list?
1->2->3->4->5->6
void fun(struct node* start)
{
if(start == NULL)
return;
printf("%d ", start->data);
if(start->next != NULL )
fun(start->next->next);
printf("%d ", start->data);
}
A)1 4 6 6 4 1
B)1 3 5 1 3 5
C)1 2 3 5
D)1 3 5 5 3 1
5) The following paradigm can be used to find the solution of the problem in minimum time:
Given a set of non-negative integer, and a value K, determine if there is a subset of the given
set with sum equal to K:
A)Divide and Conquer
B)Dynamic Programming
C)Greedy Algorithm
D)Branch and Bound
6) Which of the following sorting algorithms in its typical implementation gives best performance
when applied on an array which is sorted or almost sorted (maximum 1 or two elements are
misplaced).
A Quick Sort
B Heap Sort
C Merge Sort
D Insertion Sort
Solution : Answer (D)
7) Assume that a mergesort algorithm in the worst case takes 30 seconds for an input of size
64. Which of the following most closely approximates the maximum input size of a problem that
can be solved in 6 minutes?
(A) 256
(B) 512
(C) 1024
(D) 2048
8) If one uses straight two-way merge sort algorithm to sort the following elements in ascending
order 20, 47, 15, 8, 9, 4, 40, 30, 12, 17
then the order of these elements after the second pass of the algorithm is:
(A) 8, 9, 15, 20, 47, 4, 12, 17, 30, 40
(B) 8, 15, 20, 47, 4, 9, 30, 40, 12, 17
(C) 15, 20, 47, 4, 8, 9, 12, 30, 40, 17
(D) 4, 8, 9, 15, 20, 47, 12, 17, 30, 40
9)
Output of following Java program?
class Base {
public void Print() {
System.out.println("Base");
}
}
class Main{
public static void DoPrint( Base o ) {
o.Print();
}
public static void main(String[] args) {
Base x = new Base();
Base y = new Derived();
Derived z = new Derived();
DoPrint(x);
DoPrint(y);
DoPrint(z);
}
}
Base
A Derived
Derived
Base
B Base
Derived
Base
C Derived
Base
D Compiler Error
Solution : Answer:(A)
There are 3 units of type X, 2 units of type Y and 2 units of type Z still available. The system is
currently in a safe state. Consider the following independent requests for additional resources in
the
current state:
13) Which of the following standard algorithms is not a Greedy algorithm?
(A) Huffman Coding
(B) Prim’s algorithm
(C) Bellmen Ford Shortest path algorithm
(D)Kruskal algorithm
15) Given a sorted array of integers, what can be the minimum worst case time complexity to
find ceiling of a number x in given array? Ceiling of an element x is the smallest element present
in array which is greater than or equal to x. Ceiling is not present if x is greater than the
maximum element present in array. For example, if the given array is {12, 67, 90, 100, 300, 399}
and x = 95, then output should be 100.
(A) O(LogLogn)
(B) O(n)
(C) O(Logn)
(D) O(Logn * Logn)
(B) O(n^2)
(D) O(n^3)
17) Consider two strings A = “qpqrr” and B = “pqprqrp”. Let x be the length of the longest
common subsequence (not necessarily contiguous) between A and B and let y be the number of
(A) 33
(B) 23
(C) 43
(D) 34
18) Which of the following is true about linked list implementation of stack?
(A) In push operation, if new nodes are inserted at the beginning of linked list, then in pop
(B) In push operation, if new nodes are inserted at the end, then in pop operation, nodes must be
19) The seven elements A, B, C, D, E, F and G are pushed onto a stack in reverse order, i.e.,
starting from G. The stack is popped five times and each element is inserted into a queue.Two
elements are deleted from the queue and pushed back onto the stack. Now, one element is
(A) A
(B) B
(C) F
(D) G
(A) The AVL trees are more balanced compared to Red Black Trees, but they may cause more
(B) Heights of AVL and Red-Black trees are generally same, but AVL Trees may cause more
(C) Red Black trees are more balanced compared to AVL Trees, but may cause more rotations
(D) Heights of AVL and Red-Black trees are generally same, but Red Black rees may cause more