DSA Interview
DSA Interview
Problem Statement
You are organizing a birthday party at your house and have invited N friends. You decided to cut the
cake (which is round in shape) into exactly N+1 parts such that all your friends including you get the
piece of cake of the same size and shape. You want to find the minimum number of straight cuts you
need to make for this. (cuts can also be from center to border).
Constraints
0 ≤ N ≤ 10^18
Example
Sample Input:
Sample Output:
Explanation- To cut the round cake into quarters one has to make two cuts through the center with
an angle of 90∘ between them.
Deepank and Vowel
Problem Statement
In the realm of this quandary, Deepank wields a string S, composed of lowercase letters from the
English alphabet. This string bears a length of N. Deepank's aspiration involves a metamorphosis of S
into an exquisite string, which shall be christened as T. To actualize this, Deepank enlists the
following procedure to be executed upon each character within the string:
Should the character be a consonant, it shall undergo substitution with its nearest vowel.
If the character is already a vowel, it shall remain unaltered.
In full cognizance of the variability within the resulting string T, Deepank is immersed in
contemplation. His contemplation revolves around the tally of distinct instances of these beautiful
strings T that can be engendered through the application of the aforementioned operations upon
string S.
Given the potentially substantial scale of the answer, it is rendered in its output after being
subjected to the modulo operation with the value 10^9+7.
Note:
- There are 26 characters in the English alphabet. Five of these characters are vowels: a, e, i, o, and u.
The remaining 21 characters are consonants.
- The closest vowel to a consonant is the vowel that is least distant from that consonant. For
example, the distance between the characters d and e is 1 while the distance between the
characters d and a is 3.
- The distance between the characters z and a is 25 and not 1.
Constraints:
1 ≤ N ≤ 10^5
Sum of N over all test cases does not exceed 10^5.
Your Task -You don't need to read input or print anything. Complete the
function vowelAndDeepank() which takes String as input parameters and returns the total number
of distinct beautiful strings T that can be obtained by performing the given operations on the string
S, modulo 10^9+7.
Simple Input:
5
aeiou
Simple Output:
1
Simple Input:
5
abcde
Simple Output:
2
Test Case 1:
In this scenario:
The string S exclusively comprises vowel characters.
Therefore, no characters within the string will undergo alteration.
Consequently, the resulting string T will retain its original form: "aeiou."
Test Case 2:
Upon analysis:
The provided operations can yield two distinct strings through manipulation of string S.
These resulting strings are "aaaee" and "aaeee."
Both of these strings can be achieved by following the specified operations.
Sandy Planks
Problem Statement
Sandy, a young boy from a quaint town, has been assigned a task by his father. He possesses a
collection of N wooden planks, each numbered from 1 to N, with colors of either black or white.
Sandy’s objective is to unify the color of all the planks. However, a touch of magic resides within the
winds of his small town. Whenever he alters the color of the i-th plank (denoted by Si) to a new color
P, certain events unfold:
For 2 ≤ i ≤ N, if Si equals the color of Si-1, the color of the (i-1)th plank changes to P.
For 1 ≤ i ≤ N-1, if Si equals the color of Si+1, the color of the (i+1)th plank changes to P.
This iterative process extends to the newly colored planks. If no neighboring planks share the same
color, no changes occur to the neighbors.
Imagine Sandy’s planks with the initial coloring: BBWWWB. If Sandy modifies the color of the fourth
plank (which is white) to black, then the final coloring of the planks will be: BBBBBB.
Sandy has the liberty to select any of the N planks and alter its color as many times as needed. The
challenge is to determine the minimum number of times Ryan must repaint a plank to achieve a
uniform color across all planks.
Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 10^5
The string S consists only of characters 'B' and 'W'.
The sum of N over all test cases doesn't exceed 10^5.
Your Task - You don't need to read input or print anything. Complete the function sandyPlanks()
which takes string S of length N.Return a single integer representing the minimum number of times
Ryan has to paint a single plank to achieve a uniform color across all planks.
Simple Input:
2
6
BBWWWB
5
WWBWB
Simple Output:
1
2
Simple Input:
2
2
BB
9
WWBBBBBWW
Simple Output:
0
1
Explanation:
Test case 1: The first test case scenario is as follows, and it has already been detailed in the initial
question.
Test case 2: In this case, consider the following steps that Sandy can take to achieve uniform color
across all planks:
Sandy can paint the third plank white (W). After this step, the color sequence of planks becomes
WWWWB.
Subsequently, he can paint the third plank black (B). This leads to the following transitions:
The color of the third plank changes to black (B). The string transforms to WWBWB.
The color of the second and fourth planks also changes to black (B), resulting in WBBBB.
Finally, he paints the first plank black (B), resulting in BBBBB.
By this sequence of changes, all the planks ultimately become the same color.
Test case 3: In this case, all the planks already possess the same color, so no changes are required.
Test case 4: In this scenario, Sandy has the flexibility to select any plank numbered from 3 to 7 and
paint it white (W). The result of this action is that all the chosen planks will be colored white (W),
unifying the color across all the planks.
Stepstones
Problem Statement
It is a moment of triumph in Stepstones, as they have captured all the ships from Westeros. To
celebrate this occasion, they have prepared a problem for you to solve:
You are given an integer R, and an array A consisting of positive integers. You can do this operation
at most once (possibly zero times):
• Choose any element of the array. Replace it with any integer X, such that 1 ≤ X ≤ R.
Input
The first line consists of two space-separated integers N and R.
The second line consists of N space-separated integers – A1, A2 ... AN.
Constraints:
2 ≤ N ≤ 10^5
1 ≤ R ≤ 10^5
1 ≤ Ai ≤ 10^5
Output
Return a single integer – the maximum possible GCD of the array.
Example
Sample Input 1:
3 10
234
Sample Output 1:
2
Sample Explanation 1:
We can change the second element from 3 to 2. Then the GCD of the array becomes gcd(2, 2, 4) = 2,
which is the maximum possible.
Sample Input 2:
23
10 15
Sample Output 2:
5
Sample Explanation 2:
We do need to perform any operation. The GCD of the array is gcd(10, 15) = 5, which is the
maximum possible.
Sample Input 3:
35
11 6 12
Sample Output 3:
3
Sample Explanation 3:
We can change the first element from 11 to 3. Then the GCD of the array becomes gcd(3, 6, 12) = 3,
which is the maximum possible.
Play a game
Problem Statement
John and Olivia once start fighting when Olivia sees John talking with another girl. So, you tell them
to play the game of love in which you give them an array A of size N. Now, the game will play in turns
in which a player must decrease the value at the smallest index (with a non-zero value) in A by x (x >
0). The player who cannot make a move will lose the game.
You being a loyal friend of Olivia, wants her to win. So, tell Olivia whether she should move first or
second to win this game.
Assume both players will play optimally at every step of the game.
Input
The first line contains a single integer T (1 ≤ T ≤ 1000) — the number of test cases.
The first line of each test case contains a single integer N (1 ≤ N ≤ 100) — the size of the array A on
which the game is to be played.
The second line contains n integers A1, A2, …, AN (1 ≤ Ai ≤ 10^9).
Output
For each test case, Return on a new line whether Olivia should move "first" or "second" (without
quotes) to win the game.
Example
Sample Input:
2
3
213
2
11
Sample Output:
first
second
Sample Explanation:
For the first test case, Olivia will remove 2 from the 1st index, then John has to remove 1 from the
2nd index, and finally, Olivia will remove 3 from the 3rd index.
For the second test case, John has to remove 1 from the 1st index, then Olivia removes 1 from the
2nd index.
Input:
2
4
4321
5
12345
Output:
first
second
Explanation:
For the first test case,Olivia removes 4 from 1st index, then John removes 3 from 2nd index, Olivia
removes 2 from 3rd index, and finally John removes 1 from 4th index
For the Second test case,John removes 1 from 1st index, then Olivia removes 2 from 2nd index, John
removes 3 from 3rd index, Olivia removes 4 from 4th index, and John removes 5 from 5th index.
Geek Champion
Problem Statement
At the annual Byteland fair, Geek organized a competition with N participants, each having unique
scores (positive or negative). The task was to create groups (G1, G2, ...) so that the total score is
maximized. Each group's score was the sum of its members' scores, and the total score was the sum
of squares of these group scores. Geek wants to know the largest and smallest possible group sizes
for the maximum total score. These sizes represent the maximum and minimum sizes of groups that
can be formed while optimizing the total score.
Constraints:
- 1 ≤ N ≤ 10^5
- 1 ≤ |Ai| ≤ 10^9 for each valid i
- The sum of N over all test cases does not exceed 5 * 10^5
Your Task -You don't need to read input or print anything. Complete the function
competitionOfSharpener() which takes array as input parameters and returns an Array of size 2 who
is (Arr[0]) contain the size of the largest possible group and (Arr[1]) contain the smallest possible
group.
Simple Input:
5
52467
Simple Output:
55
Simple Input:
6
99 3 1 7 2 89
Simple Output:
66
Explanation:
Example case 1: To maximize the total score, everyone has to belong to the same group with scores
[5, 2, 4, 6, 7]. For this group, X = 5 + 2 + 4 + 6 + 7 = 24 and the total score is 24^2 = 576. The size of
the largest possible group, as well as the size of the smallest possible group, is 5.
Geek team
Problem Statement:
In the context of a coding contest, Geek is confronted with a series of N problems, each uniquely
assigned a difficulty level denoted as Ai. As problems are successfully tackled, the team is rewarded
with vibrantly colored balloons representing the problem numbers. The colors follow the VIBGYOR
sequence for problems numbered 1 to 7, while distinct hues are allocated to the remaining
challenges.
Your mission is to determine the minimum number of problems that Geek team must effectively
solve. This will enable them to acquire balloons not only for problems 1 to 7 but potentially for
more, as they methodically address problems in ascending order of difficulty.
Constraints:
7 ≤ N ≤ 15
1 ≤ Arr ≤ N
Your Task-You don't need to read input or print anything. Complete the
function sharpenerBalloon() which takes array as input parameters and returns single line
containing one integer ― the minimum number of problems Geek team needs to solve.
Simple Input:
7
1234576
Simple Output:
7
Simple Input:
8
87654321
Simple Output:
8
Simple Input:
11
11 2 4 6 8 10 1 3 5 7 9
Simple Output:
10
Explanation:
Example 1:In the first scenario, where there are a total of 7 problems, the team led by Geek is
required to solve all of these problems.
Example 2:Consider the second case, where problems numbered 1 through 7 are included within the
initial set of 8 problems.
Line Segment
Problem Statement
You have been provided a 2D grid, along with two integers X and Y.
Envision a collection "S" of coordinates (x, y) on the Cartesian plane, adhering to the constraints 1 <=
x <= N and 1 <= y <= M, where x and y are both positive integers.
Your task is to determine the count of line segments that have their endpoints within the set S, while
also satisfying the condition that the coordinates of the midpoint are also present in set S.
Problem Explanation: Imagine you have a grid like graph paper. You're given two numbers, N and M,
which determine the size of the grid. You want to find out how many straight lines you can draw on
this grid in such a way that both ends of the line and the middle point of the line fall on the dots of
the grid. In other words, you're counting how many line segments you can make where all three
points (both ends and the middle) are on the grid's dots.
Input
The first line contains one integers – T (number of test cases).
The next T lines contains two integers N, M.
Constraints:
1 ≤ T ≤ 1000
1 ≤ N, M ≤ 1000
Output
Return T lines each containing a single integer denoting the number of such line segments.
Example
INPUT 1:
2
33
67
OUTPUT 1:
8
204
Explanation:
In the first test case, the grid is of size 3x3. There are 8 line segments whose end points and
midpoints lie in the grid. In the second test case, the grid is of size 6x7, and there are 204 such line
segments.
Input 2:
1
55
Output 2:
72
Rohan Garden
Problem Statement
Rohan is tending to his garden, which boasts N apple trees. In the beginning, each tree is adorned
with Ai apples, and an additional batch of M apples awaits attachment to these trees for the purpose
of testing the laws of gravity.
Within a span of one minute, a maximum of Bi apples have the potential to descend from the ith
tree.
Kindly aid Rohan in ascertaining the shortest duration he must patiently await before comfortably
taking a seat beneath any apple tree.
Problem Explanation: Imagine Rohan has a garden with N apple trees. Each tree already has some
apples hanging on it, represented by the number Ai. He also has a batch of M apples that he wants
to attach to these trees for an experiment.
In every passing minute, each tree can drop a maximum of Bi apples due to gravity.
Rohan wants to know the shortest time he needs to wait until he can sit under any tree in his garden
without worrying about apples falling on him. In other words, he wants to figure out how long it will
take until all the apples, both those already on the trees and the ones he's adding, have fallen down.
So, the task is to help Rohan determine the minimum time he should wait before he can relax under
the trees without getting hit by falling apples.
Input
The first line contains two space-separated integers – N and M indicating the number of apple trees
and the number of apples to be attached.
Each of the next N lines contains two integers Ai and Bi specifying the initial count of apples and rate
of fall of apples per minute for ith tree.
Constraints:
1 <= N <= 2*105
0 <= Ai <= 10^6
1 <= M, Bi <= 10^6
Output
Return minimum time Rohan has to wait
Example
Input 1:
36
43
74
15
Output 1:
2
Explanation:
Attach 2, 1, 3 apples to 1st, 2nd, 3rd tree respectively. Then, 2, 2, 1 minutes are required for all
apples to fall from 1st, 2nd, 3rd tree respectively. So, Wait time is max(2, 2, 1) = 2.
Input 2:
24
52
33
Output 2:
3
Explanation:
Attach 2, 2 apples to 1st and 2nd tree respectively. Then, 3, 3 minutes are required for all apples to
fall from 1st and 2nd tree respectively. So, Wait time is max(3, 3) = 3.