You are on page 1of 32

1)Given n non-negative integers representing the histogram’s bar height where the width of

each bar is 1, find the area of largest rectangle in the histogram.

Above is a histogram where width of each bar is 1, given height = ​[2,1,5,6,2,3]​.

The largest rectangle is shown in the shaded area, which has area = ​10​ unit.

For example,

Given height = ​[2,1,5,6,2,3]​,

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?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

Example :

There is one obstacle in the middle of a 3x3 grid as illustrated below.

[
[0,0,0],
[0,1,0],
[0,0,0]
]

The total number of unique paths is ​2​.

3)(Avg Time on InterviewBit: 37 mins) Problem Score 7-8


https://www.interviewbit.com/problems/jump-game-array/
Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

For example:

A = [2,3,1,1,4]​, return ​1​ ( true ).

A = [3,2,1,0,4]​, return ​0​ ( false ).

Return 0/1 for this problem


4)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up
all the values along the path equals the given sum.
Example :

Given the below binary tree and ​sum = 22​,m

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​.

Return ​0 / 1​ ( 0 for false, 1 for true ) for this problem

6) Avg Time on InterviewBit : 28 mins (Easy problem Score 2-3)


Given an array of integers, return the highest product possible by multiplying 3 numbers from
the array ​https://www.interviewbit.com/problems/highest-product/

Input:

array of integers e.g {1, 2, 3}


​NOTE:​ Solution will fit in a 32-bit signed integer

Example:

[0, -1, 3, 100, 70, 50]

=> 70*50*100 = 350000

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]]

Input : array = {-2, 0, 1, 1, 2} sum = 10


Output : No triplets can be formed

9) Problem Score 6-7 ​https://www.interviewbit.com/problems/longest-palindromic-substring/


Given a string s, find the longest palindromic substring in s. You may assume that the maximum
length of s is 1000.
Example 1:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.

Example 2:
Input: "cbbd"
Output: "bb"

10) Hard Problem, Problem Score


Given a Linked List of length n and block length k rotate in circular manner towards right/left
each block by a number d. If d is positive rotate towards right else rotate towards left.
Examples:
Input: 1->2->3->4->5->6->7->8->9->NULL,
k=3
d=1
Output: 3->1->2->6->4->5->9->7->8->NULL
Explanation: Here blocks of size 3 are
rotated towards right(as d is positive)
by 1.

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.

11) Problem Score 5


Given a non-empty string s and a dictionary wordDict containing a list of non-empty words,
determine if s can be segmented into a space-separated sequence of one or more dictionary
words.
Note:
The same word in the dictionary may be reused multiple times in the segmentation.
You may assume the dictionary does not contain duplicate words.

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

12) Problem Score 7-8


There are a total of n courses you have to take, labeled from 0 to n-1.
Some courses may have prerequisites, for example to take course 0 you have to first take
course 1, which is expressed as a pair: [0,1]
Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish
all courses?
Example 1:
Input: 2, [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.
Example 2:
Input: 2, [[1,0],[0,1]]
Output: false
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0, and to take course 0 you should
also have finished course 1. So it is impossible.

13 ) Given a Binary Tree, find vertical sum of the nodes that are in same vertical line. Print all 

sums through different vertical lines. 

Examples: 

1
/ \
2 3
/ \ / \
4 5 6 7

The tree has 5 vertical lines 

Vertical-Line-1 has only one node 4 => vertical sum is 4 

Vertical-Line-2: has only one node 2=> vertical sum is 2 

Vertical-Line-3: has three nodes: 1,5,6 => vertical sum is 1+5+6 = 12 

Vertical-Line-4: has only one node 3 => vertical sum is 3 

Vertical-Line-5: has only one node 7 => vertical sum is 7 

So expected output is 4, 2, 12, 3 and 7 

 
 

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 

complex version can be with a limited number of moves. 

Following is an example maze. 


Gray blocks are dead ends (value = 0).

Following is binary matrix representation of the above maze. 

{1, 0, 0, 0}
{1, 1, 0, 1}
{0, 1, 0, 0}
{1, 1, 1, 1}

Following is a maze with highlighted solution path. 

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 

Given an array of size n that has the following specifications: 

1. Each element in the array contains either a policeman or a thief. 


2. Each policeman can catch only one thief. 
3. A policeman cannot catch a thief who is more than K units away from the 
policeman. 

We need to find the maximum number of thieves that can be caught. 

Examples: 

Input : arr[] = {'P', 'T', 'T', 'P', 'T'},


k = 1.
Output : 2.
Here maximum 2 thieves can be caught, first
policeman catches first thief and second police-
man can catch either second or third thief.

Input : arr[] = {'T', 'T', 'P', 'P', 'T', 'P'},


k = 2.
Output : 3.

Input : arr[] = {'P', 'T', 'P', 'T', 'T', 'P'},


k = 3.
Output : 3.
Input​: Head of following linked list 

1->2->3->4->NULL 

Output​: Linked list should be changed to, 

4->3->2->1->NULL 

Input​: Head of following linked list 

1->2->3->4->5->NULL 

Output​: Linked list should be changed to, 

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 

Input​: arr[] = {7, 10, 4, 3, 20, 15} 

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. 

We can describe the problem input as an array of numbers/characters representing 

persons in the party. We also have a hypothetical function ​HaveAcquaintance(A, B) 

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: 

is_empty(S) : Tests whether stack is empty or not.


push(S) : Adds new element to the stack.
pop(S) : Removes top element from the stack.
top(S) : Returns value of the top element. Note that this
function does not remove element from the stack.

Example: 

Input: -3 <--- Top


14
18
-5
30

Output: 30 <--- Top


18
14
-3
-5
23)​Find length of loop in linked list 
 

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 

should return 0. 

 
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

25)​K’th Smallest/Largest Element in Unsorted Array  


 
https://www.interviewbit.com/problems/kth-smallest-element-in-tree/ 
 
Given an array and a number k where k is smaller than the size of the array, we need to 
find the k’th smallest element in the given array. It is given that all array elements are 
distinct. 
Examples: 

Input: arr[] = {7, 10, 4, 3, 20, 15}


k = 3
Output: 7

Input: arr[] = {7, 10, 4, 3, 20, 15}


k = 4
Output: 10
26)Fitting Shelves Problem 

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

Solution: ​Answer:​ (​ A) 

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)

Solution: ​Answer: (B)

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

Solution: ​Answer : (D)

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

Solution: ​Answer :(D)

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

Solution : ​Answer : (B)

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

Solution: ​Answer: (B)

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

Solution : ​Answer: (B)

9)
Output of following Java program?
class Base {
public void Print() {
System.out.println("Base");
}
}

class Derived extends Base {


public void Print() {
System.out.println("Derived");
}
}

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)

10) Consider an undirected unweighted graph G. Let a breadth-first traversal of G be done 


starting from a node r. Let d(r, u) and d(r, v) be the lengths of the shortest paths from r to u and 
v respectively, in G. lf u is visited before v during the breadth-first traversal, which of the 
following statements is correct? 
(A) d(r, u) < d (r, v) 
(B) d(r, u) > d(r, v) 
(C) d(r, u) <= d (r, v) 
(D) None of the above 
 
Solution: ​Answer: (C)  
 
11)Which of the following is NOT true of deadlock prevention and deadlock avoidance 
schemes? 
(A)​ In deadlock prevention, the request for resources is always granted if the resulting state is 
safe 
(B)​ In deadlock avoidance, the request for resources is always granted if the result state is safe 
(C)​ Deadlock avoidance is less restrictive than deadlock prevention 
(D)​ Deadlock avoidance requires knowledge of resource requirements a priori 
 
 
Solution: ​Answer:​ (​ A)  
 
 
 
12) An operating system uses the Banker’s algorithm for deadlock avoidance when managing 
the allocation of three resource types X, Y, and Z to three processes P0, P1, and P2. The table 
given below presents the current system state. Here, the Allocation matrix shows the current 
number of resources of each type allocated to each process and the Max matrix shows the 
maximum number of resources of each type required by each process during its execution. 

 
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: 

REQ1: P0 requests 0 units of X,


0 units of Y and 2 units of Z
REQ2: P1 requests 2 units of X,
0 units of Y and 0 units of Z

Which one of the following is TRUE? 

(A)​ Only REQ1 can be permitted. 

(B)​ Only REQ2 can be permitted. 

(C)​ Both REQ1 and REQ2 can be permitted. 

(D)​ Neither REQ1 nor REQ2 can be permitted 

Solution: ​Answer:​ (​ B)  

 
 
 
 
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  

Solution: ​Answer:​ ​ (C) 


 
14) To implement Dijkstra’s shortest path algorithm on unweighted graphs so that it runs in 
linear time, the data structure to be used is: 
(A)​ Queue 
(B)​ Stack 
(C)​ Heap 
(D)​ B-Tree 
 
 
Solution: ​Answer:​ (​ A)  
 

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) 

Solution: ​Answer:​ (​ C)  


 
 
 
 
 
 
16) Consider a sorted array of n numbers and a number x. What would be the time complexity of 
the best known algorithm to find a triplet with sum equal to x. For example, arr[] = {1, 5, 10, 15, 
20, 30}, x = 40. Then there is a triplet {5, 15, 20} with sum 40. 
(A)​ O(n) 

(B)​ O(n^2) 

(C)​ O(n Log n) 

(D)​ O(n^3) 

Solution: ​Answer:​ (​ B)  

 
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 

such longest common subsequences between A and B. Then x + 10y = ___. 

(A)​ 33 

(B)​ 23 

(C)​ 43 

(D)​ 34 

Solution: ​Answer:​ (​ D)  

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 

operation, nodes must be removed from end. 

(B)​ In push operation, if new nodes are inserted at the end, then in pop operation, nodes must be 

removed from the beginning. 

(C)​ Both of the above 

(D)​ None of the above 

Solution: ​Answer:​ (​ D)  

 
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 

popped from the stack. The popped item is ________. 

(A)​ A 

(B)​ B 

(C)​ F 

(D)​ G 

Solution: ​Answer:​ (​ B)  

20) Which of the following is true 

(A)​ The AVL trees are more balanced compared to Red Black Trees, but they may cause more 

rotations during insertion and deletion. 

(B)​ Heights of AVL and Red-Black trees are generally same, but AVL Trees may cause more 

rotations during insertion and deletion. 

(C)​ Red Black trees are more balanced compared to AVL Trees, but may cause more rotations 

during insertion and deletion. 

(D)​ Heights of AVL and Red-Black trees are generally same, but Red Black rees may cause more 

rotations during insertion and deletion. 

Solution: ​Answer:​ (​ A)  

You might also like