0% found this document useful (0 votes)
22 views16 pages

Prep Sheet

Uploaded by

sagar awasthi
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)
22 views16 pages

Prep Sheet

Uploaded by

sagar awasthi
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/ 16

Prep-Sheet

1. Given an array "A" of N integers, you have also defined the new array
"B" as a concatenation of array "A" for an infinite number of times. For
example, if the given array "A" is [1,2,3] then, the infinite array "B" is
[1,2,3,1,2,3,1,2,3. ... ].
Now you are given Q queries, each query consists of two integers "L"
and "R"(1 based indexing). Your task is to find the sum of the subarray
from index "L" to "R" (both inclusive) in the infinite array "B" for each
query.
Note:

The value of the sum can be very large, return the answer as
modulus 10^9+7.

Click here to solve


2. You are given an integer array 'ARR' of size 'N' and an integer 'S'. Your
task is to return the list of all pairs of elements such that each sum of
elements of each pair equals 'S'.

Note:

Each pair should be sorted i.e the first value should be less than
or equals to the second value.

Return the list of pairs sorted in non-decreasing order of their


first value. In case if two pairs have the same first value, the pair
with a smaller second value should come first.

Click here to solve


3. You are given an array (ARR) of length N, consisting of integers.
You have to find the sum of the subarray (including empty subarray)
having maximum sum among, all subarrays.
A subarray is a contiguous segment of an array. In other words, a
subarray can be formed by removing 0 or more integers from the
beginning, and 0 or more integers from the end of an array.

Note:

The sum of an empty subarray is 0.

Click here to solve


4. Ninja has been given a binary string 'STR' containing either 'O' or '1'.
A binary string is called beautiful if it contains alternating Os and 1s.

For Example:

"0101', '1010', '101', '010' are beautiful strings. He wants to make


'STR' beautiful by performing some operations on it. In one operation,
Ninja can convert 'O' into 'l' or vice versa. Your task is to determine the
minimum number of operations Ninja should perform to make 'STR'
beautiful.
make 'STRO. At index of which is a Minimum operations to make
'STR' 0010' beautiful is 'l'. In one operation, we can convert 'O' at
index '0' (0-based indexing) to 'l'. The 'STR' now becomes 1010'
which is a beautiful string.

Click here to solve


5. Yogesh is a very intelligent student and is interested in research in the
Machine Learning domain. His college has only one professor, Mr. Peter
working in that field. He approaches the professor for the same, but the
professor wants to test him first. To pass the test Yogesh must answer Q
questions correctly. In all the Q questions, the professor gives him three
positive integers : A, B and K where A ≤ B. Now Yogesh has to find an
integer P such that it is closest to integer A and there are at least K
prime numbers in range [A, P], where A <= P <= B.
Help Yogesh to find and print the minimum possible P or print -1 if there
is no such possible integer.
Note:

A prime number is a natural number greater than 1 that has no


positive divisors other than 1 and itself.

Click here to solve


6. You are given an array 'ARR' of integers of length N. Your task is
to find the first missing positive integer in linear time and constant
space. In other words, find the lowest positive integer that does not
exist in the array. The array can have negative numbers as well.
For Example:

the input [3, 4, -1, 1] should give output 2 because it is the


smallest positive number that is missing in the input array.

Click here to solve


7. You are given an array 'ARR' of integers of length N. Your task is to
find the first missing positive integer in linear time and constant space.
In other words, find the lowest positive integer that does not exist in
the array. The array can have negative numbers as well.
For Example:

consider the following binary tree:


7

7 7

8 3 7

For the above tree, the length of the longest path where each
node in the path has the same value is 3 and path is 7 -> 7 -> 7 -> 7.

Click here to solve


8. For a given integer array/list 'ARR' of size 'N' containing all distinct
values, find the total number of 'Inversions that may exist. An inversion
is defined for a pair of integers in the array/list when the following two
conditions are met.

A pair ('ARR[i]', 'ARR(j') is said to be an inversion when :

1. 'ARR [ i ] > 'ARR [ j ]'


2. 'i' < 'j'

Where 'i' and 'j' denote the indices ranging from (0, 'N').

Click here to solve


9. You are given a Singly Linked List of integers and an integer array 'B' of
size 'N'. Each element in the array 'B' represents a block size. Modify the
linked list by reversing the nodes in each block whose sizes are given
by the array 'B'.

Note:

1. If you encounter a situation when 'B [ i ]' is greater than the


number of remaining nodes in the list, then simply reverse the re-
maining nodes as a block and ignore all the block sizes from 'B [ i
]'.

2. All block sizes are contiguous i.e. suppose that block 'B [ i ]' ends
at a node cur, then the block 'B [ i+1 ]' starts from the node just after
the node cur.

Click here to solve


10. You have been given a 9 X 9 2D matrix 'MATRIX' with some cells filled
with digits(1 - 9), and some empty cells (denoted by 0).
You need to find whether there exists a way to fill all the empty cells
with some digit(1 - 9) such that the final matrix is a valid Sudoku solution.

A Sudoku solution must satisfy all the following conditions-

1. Each of the digits 1 - 9 must occur exactly once in each row.


2. Each of the digits 1 - 9 must occur exactly once in each column.
3. Each of the digits 1 - 9 must occur exactly once in each of the 9, 3 x 3
sub-matrices of the matrix.

Click here to solve


11. . Given a singly linked list of integers. Your task is to return the head
of the reversed linked list.

For Example:

The given linked list is 1 -> 2 -> 3 -> 4-> NULL. Then the reverse linked
list is 4 -> 3 -> 2 -> 1 -> NULL and the head of the reversed linked list
will be 4.

Click here to solve


12. You are given an integer 'N', your task is to find and return the N'th
Fibonacci number using matrix exponentiation.
Since the answer can be very large, return the answer modulo 10^9 +7.

Fibonacci Number Is Calculated Using The Following Formula:

F(n) = F(n-1) + F(n-2),

Where, F(1) = F(2) = 1.

Click here to solve


13. You have been given a permutation of 'N' integers. A sequence of 'N'
integers is called a permutation if it contains all integers from 1 to 'N'
exactly once. Your task is to rearrange the numbers and generate the
lexicographically next greater permutation.
To determine which of the two permutations is lexicographically
smaller, we compare their first elements of both permutations. If they
are equal — compare the second, and so on. If we have two permu-
tations X and Y, then X is lexicographically smaller if X[ i ] <Y[ i ], where
'i' is the first index in which the permutations X and Y dißer.

For Example:

[2, 1, 3, 4] is lexicographically smaller than [2, 1, 4, 3].

Click here to solve


14. Ninja is planning this 'N' days-long training schedule. Each day, he can
perform any one of these three activities. (Running, Fighting Practice or
Learning New Moves).
Each activity has some merit points on each day. As Ninja has to improve
all his skills, he can't do the same activity in two consecutive days. Can
you help Ninja find out the maximum merit points Ninja can earn?
You are given a 2D array of size N*3 'POINTS' with the points correspond-
ing to each day and activity. Your task is to calculate the maximum
number of merit points that Ninja can earn.t

For Example:

If the given 'POINTS' array is [ [1,2,5] , [ 3,1,1 ] , [ 3,3,3] , the answer


will be 11 as 5 + 3 + 3.

Click here to solve


15. You are given an array/list ARR consisting of Nintegers. Your task is to
find all the distinct triplets present in the array which adds up to a given
number K.
An array is said to have a triplet {ARR[i], ARR[], ARR[k]} with sum = 'K' if
there exists three indices i, j and k such that i!=j, j!=k and i!=j and ARR[i] +
ARR[j] + ARR[k] = 'K'.

Note:

1. You can return the list of values in any order. For example, if a
valid triplet is {1, 2, -3), then {2, -3, 1), {-3, 2, 1} etc is also valid triplet.
Also, the ordering of dißerent triplets can be random i.e if there
are more than one valid triplets, you can return them in any order.
2. The elements in the array need not be distinct.
3. If no such triplet is present in the array, then return an empty list,
and the output printed for such a test case will be"-1".

Click here to solve

You might also like