0% found this document useful (0 votes)
68 views31 pages

Example:: Linked List

The document contains 4 questions related to linked lists: 1. Given a linked list with a cycle, return the node where the cycle begins. If no cycle, return null. 2. Given a linked list and integer K, reverse nodes K at a time and return the modified list. Length is divisible by K. 3. Given a linked list, reverse the order of nodes at even positions while retaining the original structure. 4. Write a program to find the node where two singly linked lists intersect, if they intersect. If they do not intersect, return null.

Uploaded by

Deepam Asnani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views31 pages

Example:: Linked List

The document contains 4 questions related to linked lists: 1. Given a linked list with a cycle, return the node where the cycle begins. If no cycle, return null. 2. Given a linked list and integer K, reverse nodes K at a time and return the modified list. Length is divisible by K. 3. Given a linked list, reverse the order of nodes at even positions while retaining the original structure. 4. Write a program to find the node where two singly linked lists intersect, if they intersect. If they do not intersect, return null.

Uploaded by

Deepam Asnani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

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

Return the node corresponding to node 3.

---------------------------------------------------------
-------------------

Q2. Given a singly linked list and an integer K, reverses the nodes
of the

list K at a time and returns modified linked list.


NOTE : The length of the list is divisible by K
Example :
Given linked list 1 -> 2 -> 3 -> 4 -> 5 -> 6 and K=2,
You should return 2 -> 1 -> 4 -> 3 -> 6 -> 5
Try to solve the problem using constant extra space.

-----------------------------------------------------------------------------------------------
--------------------------------

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.

-----------------------------------------------------------------------------------------------
----------------

Q4. Write a program to find the node at which the intersection of


two singly linked lists begins.
For example, the following two linked lists:
A: a1 → a2

c1 → c2 → c3

B: b1 → b2 → b3

begin to intersect at node c1.


Notes:
• If the two linked lists have no intersection at all, return
null.
• The linked lists must retain their original structure after
the function returns.
• You may assume there are no cycles anywhere in the
entire linked structure.
• Your code should preferably run in O(n) time and use
only O(1) memory.

STACKS AND QUEUES:

Q1 Given an integer array A of non-negative integers representing


an elevation map where the width of each bar is 1, compute how
much water it is able to trap after raining.

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:

In this case, 6 units of rain water (blue section) are


being trapped.

Explanation 2:
No water is trapped.

____________________________________________________________________
____________

Q2. Given an array of integers A of size N. A represents a histogram


i.e A[i] denotes height of
the ith histogram’s bar. Width of each bar is 1.
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.
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:

Q1) Assume that the value of a = 1, b = 2, c = 3, ... , z = 26. You are


given a numeric string S. Write a program to return the list of all
possible codes that can be generated from the given string.
Note : The order of codes are not important. And input string does
not contain 0s.
Input format :
A numeric string
Constraints :
1 <= Length of String S <= 10
Sample Input:
1123
Sample Output:
aabc
kbc
alc
aaw
kw

____________________________________________________________________

Q2) Given a string s, partition s such that every string of the


partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab",
Return
[
["a","a","b"]
["aa","b"],
]

Ordering the results in the answer :


Entry i will come before Entry j if :
• len(Entryi[0]) < len(Entryj[0]) OR
• (len(Entryi[0]) == len(Entryj[0]) AND len(Entryi[1]) <
len(Entryj[1])) OR
*
*
*
• (len(Entryi[0]) == len(Entryj[0]) AND … len(Entryi[k] <
len(Entryj[k]))

In the given example,


["a", "a", "b"] comes before ["aa", "b"] because len("a") <
len("aa")
__________________________________________________________________

Q3) Given a collection of integers that might contain duplicates, S,


return all possible subsets.
Note:
• Elements in a subset must be in non-descending order.
• The solution set must not contain duplicate subsets.
• The subsets must be sorted lexicographically.

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.

Given an integer n, return all distinct solutions to the n-queens


puzzle.
Each solution contains a distinct board configuration of the n-
queens’ placement, where 'Q' and '.' both indicate a queen and
an empty space respectively.
For example,
There exist two distinct solutions to the 4-queens puzzle:
[
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."],

["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]

_________________________________________________________

HASHING:

Q1: Determine if a Sudoku is valid, according to:


http://sudoku.com.au/TheRules.aspx
The Sudoku board could be partially filled, where empty cells are
filled with the character ‘.’.
The input corresponding to the above configuration :
["53..7....", "6..195...", ".98....6.", "8...6...3",
"4..8.3..1", "7...2...6", ".6....28.", "...419..5",
"....8..79"]

A partially filled sudoku which is valid.


Note:
• A valid Sudoku board (partially filled) is not necessarily
solvable. Only the filled cells need to be validated.

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

____________________________________________________________________

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

cat and tca are anagrams which correspond to index 1 and 4.


dog and god are another set of anagrams which correspond to index
2 and 3.
The indices are 1 based ( the first element has index 1 instead of
index 0).
Ordering of the result : You should not change the
relative ordering of the words / phrases within the group.
Within a group containing A[i] and A[j], A[i] comes
before A[j] if i < j.

__________________________________________________________________
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

with random pointers going from


1 -> 3
2 -> 1
3 -> 1

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.

____________________________________________________________________

Q3) Consider lines of slope -1 passing between nodes.


Given a Binary Tree A containing N nodes, return all diagonal
elements in a binary tree belonging to same line.
NOTE:
• See Sample Explanation for better understanding.
• Order does matter in the output.
• To get the same order as in the output traverse the tree same
as we do in pre-order traversal.

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:

1) Diagonal 1 contains [1, 2, 3]


2) Diagonal 2 contains [4, 5, 7, 6]
3) Diagonal 3 contains [8, 9]

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.

So concantenate all the array as return it as a single


one.
Final output: [1, 2, 3, 4, 5, 7, 6, 8, 9]

Explanation 2:

1) Diagonal 1 contains [11, 12, 13]


2) Diagonal 2 contains [20, 15, 17, 16]
3) Diagonal 2 contains [1, 2, 22, 34]

So concantenate all the array as return it as a single


one.
Final output: [11, 12, 13, 20, 15, 17, 16, 1, 2, 22, 34]

____________________________________________________________________

DYNAMIC PROGRAMMING

Q1) Given an array of non-negative integers, A, you are initially


positioned at the 0th 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.
Input Format:
The first and the only argument of input will be an
integer array A.

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

0 <= A[i] <= 30


Examples:
Input 1:
A = [2,3,1,1,4]

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.

____________________________________________________________________

Q3)Given a N * 2 array A where (A[i][0], A[i][1]) represents the


ith pair.
In every pair, the first number is always smaller than the second
number.
A pair (c, d) can follow another pair (a, b) if b < c , similarly in this
way a chain of pairs can be formed.
Find the length of the longest chain subsequence which can be
formed from a given set of pairs.

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:

Q1) Given two integer arrays A and B of size N.


There are N gas stations along a circular route, where the amount
of gas at station i is A[i].
You have a car with an unlimited gas tank and it costs B[i] of gas to
travel from station i
to its next station (i+1). You begin the journey with an empty tank
at one of the gas stations.
Return the minimum starting gas station’s index if you can travel
around the circuit once, otherwise return -1.
You can only travel in one direction. i to i+1, i+2, … n-1, 0, 1, 2..
Completing the circuit means starting at i and
ending up at i again.

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

Assign mouse at position x=4 to hole at position x=4 :


Time taken is 0 minutes
Assign mouse at position x=-4 to hole at position x=0 :
Time taken is 4 minutes
Assign mouse at position x=2 to hole at position x=5 :
Time taken is 3 minutes
After 4 minutes all of the mice are in the holes.

Since, there is no combination possible where the last


mouse's time is less than 4,
answer = 4.

Input:
A : list of positions of mice
B : list of positions of holes

Output:
single integer value

NOTE: The final answer will fit in a 32 bit signed integer.


____________________________________________________________________

Q3) Given an 2D integer array A of size N x 2 denoting time


intervals of different meetings.
Where:
• A[i][0] = start time of the ith meeting.
• A[i][1] = end time of the ith meeting.

Find the minimum number of conference rooms required so


that all meetings can be done.

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.

____________________________________________________________________

You might also like