Example:: Linked List
Example:: Linked List
Q1. Given a linked list, return the node where the cycle begins. If
there is no cycle, return null.
Try solving it using constant additional space.
Example :
Input :
______
| |
\/ |
1 -> 2 -> 3 -> 4
---------------------------------------------------------
-------------------
Q2. Given a singly linked list and an integer K, reverses the nodes
of the
-----------------------------------------------------------------------------------------------
--------------------------------
Q3. Given a linked list A , reverse the order of all nodes at even
positions.
Problem Constraints
1 <= Size of linked list <= 100000
Input Format
First and only argument is the head of the Linked-List A.
Output Format
Return the head of the new linked list.
Example Input
Input 1:
A = 1 -> 2 -> 3 -> 4
Input 2:
A = 1 -> 2 -> 3
Example Output
Output 1:
1 -> 4 -> 3 -> 2
Output 2:
1 -> 2 -> 3
Example Explanation
Explanation 1:
Nodes are positions 2 and 4 have been swapped.
Explanation 2:
No swapping neccassary here.
-----------------------------------------------------------------------------------------------
----------------
Problem Constraints
1 <= |A| <= 100000
Input Format
The only argument given is integer array A.
Output Format
Return the total water it is able to trap after raining.
Example Input
Input 1:
A = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
Input 2:
A = [1, 2]
Example Output
Output 1:
6
Output 2:
0
Example Explanation
Explanation 1:
Explanation 2:
No water is trapped.
____________________________________________________________________
____________
The largest rectangle is shown in the shaded area, which has area =
10 unit.
Find the area of largest rectangle in the histogram.
Input Format
The only argument given is the integer array A.
Output Format
Return the area of largest rectangle in the histogram.
For Example
Input 1:
A = [2, 1, 5, 6, 2, 3]
Output 1:
10
Explanation 1:
The largest rectangle is shown in the shaded
area, which has area = 10 unit.
________________________________________________________________
Q3) Given an array of integers A. There is a sliding window of size B
which
is moving from the very left of the array to the very right.
You can only see the w numbers in the window. Each time the
sliding window moves
rightwards by one position. You have to find the maximum for each
window.
The following example will give you more clarity.
The array A is [1 3 -1 -3 5 3 6 7], and B is 3.
Window position Max
—————————— ———————
—- —-
[1 3 -1] -3 5 3 6
3
7
1 [3 -1 -3] 5 3 6
3
7
1 3 [-1 -3 5] 3 6
5
7
1 3 -1 [-3 5 3] 6
5
7
1 3 -1 -3 [5 3 6]
6
7
1 3 -1 -3 5 [3 6
7
7]
Return an array C, where C[i] is the maximum value of from A[i] to
A[i+B-1].
Note: If B > length of the array, return 1 element with the max of
the array.
Input Format
The first argument given is the integer array A.
The second argument given is the integer B.
Output Format
Return an array C, where C[i] is the maximum value of
from A[i] to A[i+B-1]
For Example
Input 1:
A = [1, 3, -1, -3, 5, 3, 6, 7]
B = 3
Output 1:
C = [3, 3, 5, 5, 6, 7]
________________________________________________________________
BACKTRACKING:
____________________________________________________________________
Example :
If S = [1,2,2], the solution is:
[
[],
[1],
[1,2],
[1,2,2],
[2],
[2, 2]
]
_________________________________________________________
Q4) The n-queens puzzle is the problem of placing n queens on an
n×n chessboard such that no two queens attack each other.
["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]
_________________________________________________________
HASHING:
____________________________________________________________________
Q2: Given an array of strings, return all groups of strings that are
anagrams. Represent a group by a list of integers representing the
index in the original list. Look at the sample case for clarification.
Anagram : a word, phrase, or name formed by rearranging
the letters of another, such as 'spar', formed from 'rasp'
Note: All inputs will be in lower-case.
Example :
Input : cat dog god tca
Output : [[1, 4], [2, 3]]
__________________________________________________________________
Q3) A linked list is given such that each node contains an additional
random pointer which could point to any node in the list or NULL.
Return a deep copy of the list.
Example
Given list
1 -> 2 -> 3
You should return a deep copy of the list. The returned answer
should not contain the same node as the original list, but a copy of
them. The pointers in the returned list should not link to any node in
the original input list.
____________________________________________________________________
Q4) Given an array S of n integers, are there elements a, b, c,
and d in S such that a + b + c + d = target? Find all unique
quadruplets in the array which gives the sum of target.
Note:
• Elements in a quadruplet (a,b,c,d) must be in non-
descending order. (ie, a ≤ b ≤ c ≤ d)
• The solution set must not contain duplicate
quadruplets.
Example :
Given array S = {1 0 -1 0 -2 2}, and target = 0
A solution set is:
(-2, -1, 1, 2)
(-2, 0, 0, 2)
(-1, 0, 0, 1)
Also make sure that the solution set is lexicographically sorted.
Solution[i] < Solution[j] iff Solution[i][0] <
Solution[j][0] OR (Solution[i][0] == Solution[j][0]
AND ... Solution[i][k] < Solution[j][k])
__________________________________________________________________
HEAPS AND MAPS
Q1) Design and implement a data structure for LRU (Least Recently
Used) cache. It should support the following operations: get and set.
1.get(key) - Get the value (will always be positive) of the key if
the key exists in the cache, otherwise return -1.
2.set(key, value) - Set or insert the value if the key is not
already present. When the cache reaches its capacity, it should
invalidate the least recently used item before inserting the
new item.
The LRU Cache will be initialized with an integer corresponding to
its capacity. Capacity indicates the maximum number of unique
keys it can hold at a time.
Definition of “least recently used” : An access to an item is
defined as a get or a set operation of the item. “Least recently
used” item is the one with the oldest access time.
NOTE: If you are using any global variables, make sure to
clear them in the constructor.
Example :
Input :
capacity = 2
set(1, 10)
set(5, 12)
get(5) returns 12
get(1) returns 10
get(10) returns -1
set(6, 14) this pushes out key = 5 as LRU is
full.
get(5) returns -1
____________________________________________________________________
Q2) You are given an array of N integers, A1, A2 ,..., AN and an integer B. Return the
of count of distinct numbers in all windows of size B.
Formally, return an array of size N-B+1 where i'th element in this array contains
number of distinct elements in sequence Ai, Ai+1 ,..., Ai+B-1.
NOTE: if B > N, return an empty array.
Input Format
First argument is an integer array A
Second argument is an integer B.
Output Format
Return an integer array.
Example Input
Input 1:
A = [1, 2, 1, 3, 4, 3]
B = 3
Input 2:
A = [1, 1, 2, 2]
B = 1
Example Output
Output 1:
[2, 3, 3, 2]
Output 2:
[1, 1, 1, 1]
Example Explanation
Explanation 1:
A=[1, 2, 1, 3, 4, 3] and B = 3
All windows of size B are
[1, 2, 1]
[2, 1, 3]
[1, 3, 4]
[3, 4, 3]
So, we return an array [2, 3, 3, 2].
Explanation 2:
Window size is 1, so the output array is [1, 1, 1, 1].
____________________________________________________________________
TREES:
Q1) Given an undirected tree with an even number of nodes.
Consider each connection between a parent and child node to be an
edge.
You need to remove maximum number of these edges, such that
the disconnected subtrees that remain each have an even number
of nodes.
Return the maximum number of edges you can remove.
Problem Constraints
2 <= A <= 105
1 <= B[i][0], B[i][1] <= A
Integer A will be even.
Input Format
First argument is an integer A denoting the number of nodes in the
tree.
Second argument is a 2D array B of size (A-1) * 2, denoting the
edge between nodes B[i][0] and B[i][1].
Output Format
Return an integer, denoting the maximum number of edges you can
remove.
Example Input
Input 1:
A = 6
B = [
[1, 2]
[1, 3]
[1, 4]
[3, 5]
[4, 6]
]
Input 2:
A = 2
B = [
[1, 2]
]
Example Output
Output 1:
2
Output 2:
0
Example Explanation
Explanation 1:
1
/ | \
2 3 4
| \
5 6
Maximum number of edges we can remove is 2, i.e (1, 3)
and (1, 4)
Explanation 2:
We can't remove any edges.
____________________________________________________________________
Q2) Given a binary tree A with N nodes.
You have to remove all the half nodes and return the final binary
tree.
NOTE:
• Half nodes are nodes which have only one child.
• Leaves should not be touched as they have both children as
NULL.
Problem Constraints
1 <= N <= 105
Input Format
First and only argument is an pointer to the root of binary tree A.
Output Format
Return a pointer to the root of the new binary tree.
Example Input
Input 1:
1
/ \
2 3
/
4
Input 2:
1
/ \
2 3
/ \ \
4 5 6
Example Output
Output 1:
1
/ \
4 3
Output 2:
1
/ \
2 6
/ \
4 5
Example Explanation
Explanation 1:
The only half node present in the tree is 2 so we will
remove this node.
Explanation 2:
The only half node present in the tree is 3 so we will
remove this node.
____________________________________________________________________
Problem Constraints
0 <= N <= 105
Input Format
First and only Argument represents the root of binary tree A.
Output Format
Return a 1D array denoting the diagonal traversal of the tree.
Example Input
Input 1:
1
/ \
4 2
/ \ \
8 5 3
/ \ /
9 7 6
Input 2:
11
/ \
20 12
/ \ \
1 15 13
/ \ /
2 17 16
\ /
22 34
Example Output
Output 1:
[1, 2, 3, 4, 5, 7, 6, 8, 9]
Output 2:
[11, 12, 13, 20, 15, 17, 16, 1, 2, 22, 34]
Example Explanation
Explanation 1:
NOTE:
The order in the output matters like for Example:
6 and 7 belong to same diagonal i.e diagonal 2 but as 7
comes before 6 in pre-order traversal so 7 will be added
to answer array first.
Explanation 2:
____________________________________________________________________
DYNAMIC PROGRAMMING
Output Format:
Return an integer, representing the answer as described
in the problem statement.
=> 0 : If you cannot reach the last index.
=> 1 : If you can reach the last index.
Constraints:
1 <= len(A) <= 106
Output 1:
1
Explanation 1:
Index 0 -> Index 2 -> Index 3 -> Index 4
Input 2:
A = [3,2,1,0,4]
Output 2:
0
Explanation 2:
There is no possible path to reach the last index.
____________________________________________________________________
Q2) Given two integer arrays A and B of size N each which represent
values and weights associated with N items respectively.
Also given an integer C which represents knapsack capacity.
Find out the maximum value subset of A such that sum of the
weights of this subset is smaller than or equal to C.
NOTE:
• You cannot break an item, either pick the complete item, or
don’t pick it (0-1 property).
Problem Constraints
1 <= N <= 103
1 <= C <= 103
1 <= A[i], B[i] <= 103
Input Format
First argument is an integer array A of size N denoting the values
on N items.
Second argument is an integer array B of size N denoting the
weights on N items.
Third argument is an integer C denoting the knapsack capacity.
Output Format
Return a single integer denoting the maximum value subset of A
such that sum of the weights of this subset is smaller than or equal
to C.
Example Input
Input 1:
A = [60, 100, 120]
B = [10, 20, 30]
C = 50
Input 2:
A = [10, 20, 30, 40]
B = [12, 13, 15, 19]
C = 10
Example Output
Output 1:
220
Output 2:
0
Example Explanation
Explanation 1:
Taking items with weight 20 and 30 will give us the
maximum value i.e 100 + 120 = 220
Explanation 2:
Knapsack capacity is 10 but each item has weight greater
than 10 so no items can be considered in the knapsack
therefore answer is 0.
____________________________________________________________________
Problem Constraints
1 <= N <= 103
1 <= A[i][0] < A[i][1] <= 104
Input Format
First and only argument is an 2D integer array A of size N * 2
representing the N pairs.
Output Format
Return a single integer denoting the length of longest chain
subsequence of pairs possible under given constraint.
Example Input
Input 1:
A = [ [5, 24]
[39, 60]
[15, 28]
[27, 40]
[50, 90]
]
Input 2:
A = [ [10, 20]
[1, 2]
]
Example Output
Output 1:
3
Output 2:
1
Example Explanation
Explanation 1:
Longest chain that can be formed is of length 3, and the
chain is [ [5, 24], [27, 40], [50, 90] ]
Explanation 2:
Longest chain that can be formed is of length 1, and the
chain is [ [1, 2] ] or [ [10, 20] ]
____________________________________________________________________
GREEDY ALGORITHM:
Input Format
The first argument given is the integer array A.
The second argument given is the integer array B.
Output Format
Return the minimum starting gas station's index if you
can travel around the circuit once, otherwise return -1.
For Example
Input 1:
A = [1, 2]
B = [2, 1]
Output 1:
1
Explanation 1:
If you start from index 0, you can fill in A[0] =
1 amount of gas. Now your tank has 1 unit of gas. But you
need B[0] = 2 gas to travel to station 1.
If you start from index 1, you can fill in A[1] =
2 amount of gas. Now your tank has 2 units of gas. You
need B[1] = 1 gas to get to station 0. So, you travel to
station 0 and still have 1 unit of gas left over. You
fill in A[0] = 1 unit of additional gas, making your
current gas = 2. It costs you B[0] = 2 to get to station
1, which you do and complete the circuit.
____________________________________________________________________
Q2) There are N Mice and N holes are placed in a straight line.
Each hole can accomodate only 1 mouse.
A mouse can stay at his position, move one step right from x to x +
1, or move one step left from x to x − 1. Any of these moves
consumes 1 minute.
Assign mice to holes so that the time when the last mouse gets
inside a hole is minimized.
Example:
positions of mice are:
4 -4 2
positions of holes are:
4 0 5
Input:
A : list of positions of mice
B : list of positions of holes
Output:
single integer value
Problem Constraints
1 <= N <= 10
0 <= A[i][0] < A[i][1] <= 2 * 109
Input Format
The only argument given is the matrix A.
Output Format
Return the minimum number of conference rooms required so that
all meetings can be done.
Example Input
Input 1:
A = [ [0, 30]
[5, 10]
[15, 20]
]
Input 2:
A = [ [1, 18]
[18, 23]
[15, 29]
[4, 15]
[2, 11]
[5, 13]
]
Example Output
Output 1:
2
Output 2:
4
Example Explanation
Explanation 1:
Meeting one can be done in conference room 1 form 0 -
30.
Meeting two can be done in conference room 2 form 5 -
10.
Meeting one can be done in conference room 2 form 15 -
20 as it is free in this interval.
Explanation 2:
Meeting one can be done in conference room 1 from 1 - 18.
Meeting five can be done in conference room 2 from 2 -
11.
Meeting four can be done in conference room 3 from 4 -
15.
Meeting six can be done in conference room 4 from 5 -
13.
Meeting three can be done in conference room 2 from 15 -
29 as it is free in this interval.
Meeting two can be done in conference room 4 from 18 -
23 as it is free in this interval.
____________________________________________________________________
GRAPHS:
Q1) There are a total of A courses you have to take, labeled from 1
to A.
Some courses may have prerequisites, for example to take course 2
you have to first take course 1, which is expressed as a pair: [1,2].
Given the total number of courses and a list of prerequisite pairs, is
it possible for you to finish all courses?
Return 1 if it is possible to finish all the courses, or 0 if it is not
possible to finish all the courses.
Input Format:
The first argument of input contains an integer A,
representing the number of courses.
The second argument of input contains an integer array,
B.
The third argument of input contains an integer array, C.
Output Format:
Return a boolean value:
1 : If it is possible to complete all the courses.
0 : If it is not possible to complete all the
courses.
Constraints:
1 <= A <= 6e4
1 <= length(B) = length(C) <= 1e5
1 <= B[i], C[i] <= A
Example:
Input 1:
A = 3
B = [1, 2]
C = [2, 3]
Output 1:
1
Explanation 1:
It is possible to complete the courses in the
following order:
1 -> 2 -> 3
Input 2:
A = 2
B = [1, 2]
C = [2, 1]
Output 2:
0
Explanation 2:
It is not possible to complete all the courses.
____________________________________________________________________
Q2) Rishabh takes out his Snakes and Ladders Game, stares the
board and wonders: "If I can always roll the die to whatever number
I want, what would be the least number of rolls to reach the
destination?"
RULES:
• The game is played with cubic dice of 6 faces numbered
from 1 to A.
• Starting from 1 , land on square 100 with the exact roll of the
die. If moving the number rolled would place the player
beyond square 100, no move is made.
• If a player lands at the base of a ladder, the player must climb
the ladder. Ladders go up only.
• If a player lands at the mouth of a snake, the player must go
down the snake and come out through the tail. Snakes go
down only.
BOARD DESCRIPTION:
• The board is always 10 x 10 with squares numbered
from 1 to 100.
• The board contains N ladders given in a form of 2D matrix A of
size N * 2 where (A[i][0], A[i][1]) denotes a ladder that has
its base on square A[i][0] and end at square A[i][1].
• The board contains M snakes given in a form of 2D matrix B of
size M * 2 where (B[i][0], B[i][1]) denotes a snake that has
its mouth on square B[i][0] and tail at square B[i][1].
Problem Constraints
1 <= N, M <= 15
1 <= A[i][0], A[i][1], B[i][0], B[i][1] <= 100
A[i][0] < A[i][1] and B[i][0] > B[i][1]
Neither square 1 nor square 100 will be the starting point of a
ladder or snake.
A square will have at most one endpoint from either a snake or a
ladder.
Input Format
First argument is a 2D matrix A of size N * 2 where (A[i][0], A[i]
[1]) denotes a ladder that has its base on square A[i][0] and end
at square A[i][1].
Second argument is a 2D matrix B of size M * 2 where (B[i][0],
B[i][1]) denotes a snake that has its mouth on square B[i][0] and
tail at square B[i][1].
Output Format
Return the least number of rolls to move from start to finish on a
separate line. If there is no solution, return -1.
Example Input
Input 1:
A = [ [32, 62]
[42, 68]
[12, 98]
]
B = [ [95, 13]
[97, 25]
[93, 37]
[79, 27]
[75, 19]
[49, 47]
[67, 17]
Input 2:
A = [ [8, 52]
[6, 80]
[26, 42]
[2, 72]
]
B = [ [51, 19]
[39, 11]
[37, 29]
[81, 3]
[59, 5]
[79, 23]
[53, 7]
[43, 33]
[77, 21]
Example Output
Output 1:
3
Output 2:
5
Example Explanation
Explanation 1:
The player can roll a 5 and a 6 to land at square 12.
There is a ladder to square 98. A roll of 2 ends the
traverse in 3 rolls.
Explanation 2:
The player first rolls 5 and climbs the ladder to square
80. Three rolls of 6 get to square 98.
A final roll of 2 lands on the target square in 5 total
rolls.
____________________________________________________________________