You are on page 1of 14

CODING QUESTIONS

Q1. For the purpose of donating to an orphanage, you are collecting clothes from all the houses
along a street. Each house has a certain number of clothes spared to donate, the only
constraint stopping you from taking clothes from all of the houses is that as per the society
rules, no two adjacent houses can donate to the same orphanage, and hence you cannot take
clothes from any two adjacent houses. Given an integer array clothes representing the number
of clothes each house has to donate, return the maximum number of clothes you can collect
today without breaking society rules.

Example 1:
Scenario where input clothes = 1,2,3,1
Input:
1,2,3,1
Output: 4
Explanation:
Case 1 :
Clothes from house 1 (1) and clothes from house 3 (3). Total clothes = 1 + 3 = 4.
Case 2 : Clothes from house 2 (2) and clothes from house 4 (1). Total clothes = 2 + 1 = 3.
Maximum of 4 and 3 is 4, and hence the output is 4.

Example 2:
Scenario where input cloths = 2,7,9,3,1
Input:
2,7,9,3,1
Output: 12
Explanation:
Case 1: Clothes from house 1 (2), house 3 (9) and house 5 (1) = 12
Case 2: Clothes from house 2 (7) and house 5 (1) = 8
Maximum of 12 and 8 is 12, hence the output is 12.
Constraints:
1 <= clothes.length <= 100
0 <= clothes[i] <= 400

important:
Please do not change the upper part of your code template since any changes my effect the
input & output codes.
Please write your solution within the function solve_function() and return the appropriate
result from that function.

Q2. At the store, there are n toys. Jake, who is a 5 year old boy, wants to buy some of those.
You are given an array costs of length n, where costs[i] is the cost of the ith toy in coins. Jake
initially has coins coins to spend, and he wants to buy as many toys as possible.
Return the maximum number of toys Jake can buy with coins coins.
Note: Jake can buy the toys in any order.

Example 1:
Scenario where Input costs = 1,3,2,4,1 and coins = 7
Input:
1,3,2,4,1
7
Output: 4
Explanation: The boy can buy toys at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7.
Example 2:
Scenario where Input costs = 10,6,8,7,7,8 and coins = 5
Input:
10,6,8,7,7,8
5
Output: 0
Explanation: Jake cannot afford any of the toys.
Example 3:
Scenario where Input costs = 1,6,3,1,2,5 and coins = 20
Input:
1,6,3,1,2,5
20
Output: 6

Explanation: Jake can buy all the toys for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18.

Constraints:
costs.length == n
1 <= n <= 100
1 <= costs[i] <= 100
1 <= coins <= 100

important:
Please do not change the upper part of your code template since any changes my effect the
input & output codes.
Please write your solution within the function solve_function() and return the appropriate
result from that function.

Q3. You’re selling candies outside a primary school. Each candy costs INR 5. Children are buying
from you one at a time (in the order specified by coins). Each child will only buy one candy and
pay with either an INR 5, INR 10, or INR 20 coin (Assume there are valid INR 20 coins in
circulation). You have to give back the correct change to each child so that the net transaction is
the child paying INR 5 to you. Please note that when you start selling, you do not have any
change in hand at first.
Given an integer array coins where coins[i] is the coin the ith child pays, return true if you can
give back every child with the correct change, or false otherwise.

Example 1:
Scenario where input coins = 5,5,5,10,20
Input:
5,5,5,10,20
Output: true
Explanation: From the first 3 children, we collect three INR 5 coins in order (and do not give
anything back). From the fourth child, we collect an INR 10 coin and give back an INR 5 coin.
From the fifth child, we give an INR 10 coin and an INR 5 coin. Since all children got the correct
change, we output ‘true’.
Example 2:
Scenario where input coins = 5,5,10,10,20
Input:
5,5,10,10,20
Output: false
Explanation: From the first two children in order, we collect two INR 5 coins. For the next two
children in order, we collect an INR 10 coin and give back an INR 5 coin. For the last child, we
can not give the change of INR 15 back because we only have two INR 10 coins. Since not every
child received the correct change, we output ‘false’.

Constraints:
1 <= coins.length <= 100
coins[i] is either 5, 10, or 20.

important:
Please do not change the upper part of your code template, since any changes made may effect
the input & output codes.
Please write your solution within the function solve_function() and return the appropriate
result from that function.

Q4. Bella has a number of pebbles with a random integer printed on each. The game she is
playing needs her to rearrange the pebbles into groups so that each group is of size groupSize,
and consists of groupSize consecutive numbered pebbles.
Given an integer array pebbles where pebbles[i] is the value written on the ith pebble and an
integer groupSize, return true if she can rearrange the pebbles as the game needs, or false
otherwise.

Example 1:
Scenario where input pebbles = 1,2,3,6,2,8,4,7,3 and groupSize = 3
Input:
1,2,3,6,2,8,4,7,3
3
Output: true
Explanation: Bella's pebbles can be rearranged as [1,2,3],[2,3,4],[6,7,8]
Example 2:
Scenario where input pebbles = 1,2,3,4,5 and groupSize=4
Input:
1,2,3,4,5
4
Output: false
Explanation: Bella's pebbles can not be rearranged into groups of 4.

Constraints:
1 <= pebbles.length <= 100
0 <= pebbles[i] <= 100
1 <= groupSize <= pebbles.length

important:
Please do not change the upper part of your code template since any changes made may effect
the input & output codes.
Please write your solution within the function solve_function() and return the appropriate
result from that function.

Q.5 You have statements from a conversation. A statement is a list of words that are separated
by a single space with no leading or trailing spaces. For example, "Hello World", "HELLO", "hello
world hello world" are all sentences. Words consist of only uppercase and/or lowercase English
letters.
Two statements statement1 and statement2 resemble each other if it is possible to insert an
arbitrary statement (possibly empty) inside one of these statements such that the two
statements become equal.
For example, statement1 = "Hello my name is Elle" and statement2 = "Hello Elle" can be made
equal by inserting "my name is" between "Hello" and "Elle" in statement2 . Hence they
resemble each other.
Given two statements statement1 and statement2, return true if they resemble. Otherwise,
return false.
Example 1:
Scenario where input statement1 = "My name is John", statement2 = "My John"
Input:
My name is John
My John
Output: true
Explanation: statement2 can be turned to statement1 by inserting "name is" between "My" and
"John".
Example 2:
Scenario where input coins = statement1 = "of", statement2 = "A lot of birds"
Input:
of
A lot of birds
Output: false
Explanation: No single statement can be inserted inside one of the statements to make it equal
to the other.
Example 3:
Scenario where input coins = statement1 = "Eating right now", statement2 = "Eating"
Input:
Eating right now
Eating
Output: true
Explanation: statement2 can be turned to statement1 by inserting "right now" at the end of the
statement2.
Constraints:
1 <= statement1.length, statement2.length <= 100
statement1 and statement2 consist of lowercase and/or uppercase English letters and spaces.
The words in statement1 and statement2 are separated by a single space.

Q.6 A monkey is crossing a farm. The farm is divided into some number of units, and at each
unit, there may or may not exist a tree. The monkey can jump on a tree, but it must not jump
down on the land.
Given a list of trees’ positions (in units) in sorted ascending order, determine if the monkey can
cross the farm by landing on the last tree. Initially, the monkey is on the first tree and assume
the first jump must be 1 unit.
If the monkey's last jump was k units, its next jump must be either k - 1, k, or k + 1 units. The
monkey can only jump in the forward direction.

Example 1:
Scenario where input trees = 0,1,3,5,6,8,12,17
Input:
0,1,3,5,6,8,12,17
Output: true
Explanation: The monkey is initially at the first tree (at 0th unit), and can reach second tree by
jumping 1 unit, then 2 units to the third tree, then 2 units to the 4th tree, then 3 units to the
6th tree, 4 units to the 7th tree, and 5 units to the 8th tree.

Example 2:
Scenario where input trees = 0,1,2,3,4,8,9,11
Input:
0,1,2,3,4,8,9,11
Output: false
Explanation: There is no way to jump to the last tree as the gap between the 5th and 6th tree is
too large.

Constraints:
2 <= trees.length <= 2000
0 <= trees[i] <= 231 - 1
trees[0] == 0
trees is sorted in an increasing order.

Important:
Please do not change the upper part of your code template, since any changes made may affect
the input & output codes.
Please write your solution within the function solve_function() and return the appropriate
result from that function.

Q. 7 For the registration for a job interview at Amazon, there is a long queue, with every person
standing within a 2 meter block marked on the floor. Due to COVID 19, the government has
restricted any two people to be less than 4 meters apart, hence no two people can stand in the
adjacent blocks.
Given an integer array queue containing 0's and 1's, where 0 means empty block and 1 means a
person standing on that block, and an integer n, return true if n new people can be
accommodated in the queue without violating the ‘no people on adjacent blocks’ rule. Return
false otherwise.

Example 1:
Scenario where input queue = [1,0,0,0,1] and n = 1
Input:
1,0,0,0,1
1
Output: true

Example 2:
Scenario where input queue = 1,0,0,0,1 and n = 2
Input:
1,0,0,0,1
2
Output: false

Constraints:
1 <= queue.length <= 2 * 104
queue[i] is 0 or 1.
There are no two adjacent flowers in queue.
0 <= n <= queue.length

important:
Please do not change the upper part of your code template since any changes my effect the
input & output codes.
Please write your solution within the function solve_function() and return the appropriate
result from that function.

Q.8 You are visiting a dinner buffet that has a single row of dishes arranged from left to right.
The dishes are represented by an integer array dishes where dishes[i] is the type of the ith dish.
For example, dishes[1,2,1] would mean that the first and third dishes are the same.
You want to collect as many spoonfuls of dishes as possible. However, the owner has some
strict rules that you must follow:
You only have two plates, and each plate can only hold a single type of dish. There is no limit on
the number of spoonfuls of dishes each basket can hold.
Starting from any dish of your choice, you must pick exactly one spoonful from every dish
(including the start dish) while moving to the right. The picked dishes must fit in one of your
plates.
Once you reach a dish which cannot fit in your baskets, you must stop.
Given the integer array dishes, return the maximum number of spoonfuls you can pick.

Example 1:
Scenario where input dishes = 1,2,1
Input:
1,2,1
Output: 3
Explanation: We can pick from all 3 dishes.

Example 2:
Scenario where input dishes = 0,1,2,2
Input:
0,1,2,2
Output: 3
Explanation: We can pick from dishes[1,2,2].
If we had started at the first dish, we would only pick from dishes [0,1].

Example 3:
Scenario where input dishes = 1,2,3,2,2
Input:
1,2,3,2,2
Output: 4
Explanation: We can pick from dishes [2,3,2,2].
If we had started at the first dish, we would only pick from dishes [1,2].
Constraints:
1 <= dishes.length <= 105
0 <= dishes[i] < dishes.length

important:
Please do not change the upper part of your code template since any changes my effect the
input & output codes.
Please write your solution within the function solve_function() and return the appropriate
result from that function.

Q.9 We distribute some number of flowers, to a row of n people in the following way:
We then give 1 flower to the first person, 2 flowers to the second person, and so on until we
give n flowers to the last person.
Then, we go back to the start of the row, giving n + 1 flowers to the first person, n + 2 flowers to
the second person, and so on until we give n+n flowers to the last person.
This process repeats (with us giving one more flower each time, and moving to the start of the
row after we reach the end) until we run out of flowers. The last person will receive all of our
remaining flowers (not necessarily one more than the previous time).
Return an array (of length n and sum flowers) that represents the final distribution of flowers

Example 1:
Scenario where Input flowers = 7, n = 4
Input:
7
4
Output: 1,2,3,1
Explanation:
On the first turn, ans[0] += 1, and the array is [1,0,0,0].
On the second turn, ans[1] += 2, and the array is [1,2,0,0].
On the third turn, ans[2] += 3, and the array is [1,2,3,0].
On the fourth turn, ans[3] += 1 (because there is only one flower left), and the final array is
[1,2,3,1].

Example 2:
Scenario where Input flowers = 10, n = 3
Input:
10
3
Output: 5,2,3
Explanation:
On the first turn, ans[0] += 1, and the array is [1,0,0].
On the second turn, ans[1] += 2, and the array is [1,2,0].
On the third turn, ans[2] += 3, and the array is [1,2,3].
On the fourth turn, ans[0] += 4, and the final array is [5,2,3].

Constraints:
1 <=flowers<= 10^9
1 <= n <= 1000

important:
Please do not change the upper part of your code template since any changes my effect the
input & output codes.
Please write your solution within the function solve_function() and return the appropriate
result from that function.

Q.10 Jasper has a bank of coins in which there are coins of denominations given in an integer
array coins. Also, given is an integer amount representing a total amount of money Jasper
needs to collect.
Print the least number of coins that Jasper will need to collect that amount from the bank of
coins. If that amount of money cannot be made up by any combination of the coins in the bank,
print-1.
You may assume that the bank has an infinite number of coins of each denomination given in
the array.

Example 1:
Scenario where Input coins = 1,2,5 and amount = 11
Input:
1,2,5
11
Output: 3
Explanation: 11 = 5 + 5 + 2
Example 2:
Scenario where Input coins = 2 and amount = 999
Input:
2
-1
Output: 0
Example 3:
Scenario where Input costs = 1 and amount= 0
Input:
1
0
Output: 0

Constraints:
1 <= coins.length <= 12

important:
Please do not change the upper part of your code template since any changes my effect the
input & output codes.
Please write your solution within the function solve_function() and return the appropriate
result from that function.

You might also like