HackWithInfy Practice Questions 2025 - Part 1
HackWithInfy Practice Questions 2025 - Part 1
SAMPLE QUESTIONS
1. EASY
You’re given an array A of n integers and q queries.
for query 4 -> (2, 3, 4)
calculate sum of array from index 3 to 4 -> sum =
Each query can be one of the following two types: A[3]+A[4] = 28
• Type 1 Query: (1, l, r) - Replace A[i] with
(i-l+1)*A[l] for each index i, where l <= i <= r. for query 5 -> (2, 3, 3)
• Type 2 Query: (2, l, r) - Calculate the sum of the calculate sum of array from index 3 to 3 -> sum =
elements in A from index l to index r. A[3] = 12
Find the sum of answers to all type 2 queries. Since Hence, answer is 20+28+12 = 60
answer can be large, return it modulo 109+7.
Sample Input 2
Sample Input 1 7
7 3
1 7
4 4
5 2
1 5
6 3
7 7
8 5
5 104
116 201
115 136
255 233
234 205
233
Sample Output 2
Sample Output 1 111
60
Sample Output Description 2
Sample Output Description 1 Here, n = 7
Here, n = 7 A = [3, 7, 4, 2, 5, 3, 7]
A = [1, 4, 5, 1, 6, 7, 8] q=5
q=5 queries = [[1, 0, 4], [2, 0, 1], [1, 3, 6], [2, 3, 3], [2, 0, 5]]
queries = [[1, 1, 6], [1, 1, 5], [2, 5, 5], [2, 3, 4], [2, 3, 3]]
for query 1 -> (1, 0, 4)
for query 1 -> (1, 1, 6) Applying the operation on subarray from index 0 to
Applying the operation on subarray from index 1 to 4, A becomes, A = [3, 6, 9, 12, 15, 3, 7]
6, A becomes, A = [1, 4, 8, 12, 16, 20, 24]
for query 2 -> (2, 0, 1)
for query 2 -> (1, 1, 5) calculate sum of array from index 0 to 1 -> sum =
Applying the operation on subarray from index 1 to A[0]+A[1] = 9
5, A becomes, A = [1, 4, 8, 12, 16, 20, 24]
for query 3 -> (1, 3, 6)
for query 3 -> (2, 5, 5) Applying the operation on subarray from index 3 to
calculate sum of array from index 5 to 5 -> sum = 6, A becomes, A = [3, 6, 9, 12, 24, 36, 48]
A[5] = 20
Sample Input 1
11
2
1
2
2
3
2
3
5
1
2
1
1
for query 1 -> (2, 0, 3)
calculate sum of array from index 0 to 3 -> sum = Sample Output 1
A[0]+A[1]+A[2]+A[3] = 25 12
To avoid disturbance for Person 1, we need at least 1. Person 1 wants to sell 1 liter of oil (A[1] =1).
1 liter in the tank initially. Initially, the tank is empty, so Person 1 can sell 1
liter. The tank now has 1 liter of oil.
After Person 1 buys 1 liter, the tank will be empty. 2. Person 2 wants to sell 1 liter of oil (A[2]=1).
The tank now has 1 liter, so Person 2 can sell 1
Person 2 sells 1 liter, so the tank will have 1 liter. liter. The tank now has 2 liters of oil.
3. Person 3 wants to sell 1 liter of oil (A[3]=1).
Person 3 sells another liter, so the tank will have 2 The tank now has 2 liters, so Person 3 can sell 1
liters. liter. The tank now has 3 liters of oil, which is its
full capacity.
The minimum initial amount X needed to achieve 4. Person 4 wants to sell 1 liter of oil (A[4]=1).
the least number of disturbances is 1 liter. The tank is now full (3 liters), so Person 4 cannot
sell 1 liter and will be upset.
Case 2
Input: Given that the tank capacity C is 3 liters and all
3 actions are selling oil, the only disturbance occurs
2 when Person 4 tries to sell oil to a full tank.
-1
-1 Therefore, no initial amount of oil X will change the
1 disturbance for Person 4, as the tank’s capacity is
Output: already limiting.
2
Explanation: Hence, the minimum initial amount of oil X required
Given N = 3, C = 2, A = [-1, -1, 1]. to achieve the least number of disturbances
remains 0.
To ensure that there are no disturbances:
Case 3
Input:
4
3
1
1
1
1
Constraints
1 <= N <= 10^9
Case 1
Input:
5
Output:
3
Explanation:
Given N = 5.
Case 2
Input:
1
of an N×M grid behind enemy lines given by a 2D After 2 seconds the enemy’s country will be like Q =
array Q. [[“AA”], [“AA”]].
The grid consists of cells represented by the Hence, the answer for this case is equal to 2.
following characters:
1. ‘*’: Represents a block cell that cannot be visited. Case 2
2. ‘A’: Represents a cell that has been invaded by Input:
General Ali’s army. 3
3. ‘E’: Represents a cell that contains the enemy’s 2
army. AE
*E
General Ali’s invasion progresses as follows: At each EE
second, any cell marked ‘E’ that shares a side with a Output:
cell marked ‘A’ is invaded by General Ali’s army. 4 Explanation:
Given N = 3, M = 2, Q = [[“AE”], [“*E”], [“EE”]].
Find the minimum time it will take for General Ali’s
army to invade all enemy cells (‘E’) in the grid. If it’s After 1 second the enemy’s country will be like Q =
not possible to invade all enemy cells, return −1. [[“AA”], [“AE”], [“EE”]].
For some reason, the company’s building is a bit So, expert number = 3+0 = 3
weird.
• It has one office on each floor and in each Please note that there is no other way to divide the
office works one employee. employees such that expert number is greater than
• Each employee i works on the ith floor and 3.
has skill Ai.
• Each employee can belong to at most one Hence, the maximum value of expert number is 3.
team.
• Each team should have employees working Sample Input 2
in consecutive floors from i to j. In other words, 5
the teams should be divided in such a way that no 0
employee of one team can walk into the project 1
space of another team. 2
1
ABC uses a metric which is called the expert 0
number which is calculated as the sum of all the
absent expert values from each team of employees. Sample Output 2
The absent expert value of each team is the first skill 5
starting from 0 which is not present in the team.
Sample Output Description 2
It is given that a bigger expert number is a better Here, N = 5
expert number. Hence, you need to divide the A = [0, 1, 2, 1, 0]
employees into teams such that the company’s
expert number is as large as possible. We can divide the employees in the following teams
-> [0, 1, 2], [1, 0]
Find the maximum expert number that can be
obtained. expert value of team [0, 1, 2] = 3
expert value of team [1, 0] = 2
Sample Input 1
4 So, expert number = 3+2 = 5
0
2 Please note that there is no other way to divide the
1 employees such that expert number is greater than
1 5.
Sample Output Description 3 Please note that there is no other way to divide the
Here, N = 10 employees such that expert number is greater than
A = [0, 1, 0, 1, 1, 0, 3, 2, 1, 0] 3.
We can divide the employees in the following teams Hence, the maximum value of expert number is 3.
-> [0, 1], [0, 1], [1, 0, 3, 2], [1, 0]
Case 2
expert value of team [0, 1] = 2 Input:
expert value of team [1, 0, 3, 2] = 4 5
0
So, expert number = 2 + 2 + 4 + 2 = 10 1
2
Please note that there is no other way to divide the 1
employees such that expert number is greater than 0
10. Output:
5
Hence, the maximum value of expert number is 10. Explanation:
Here, N = 5
Input Format A = [0, 1, 2, 1, 0]
The first line contains an integer, N, denoting the
number of elements in A.
Each line i of the N subsequent lines (where 0 ≤ i <
N) contains an integer describing A[i].
Constraints
1 <= N <= 10^5
1 <= A[i] <= 10^3
Sample Test Cases
Case 1
Input:
4
0
2
1
1
Output:
3
Explanation:
External Document © 2025 Infosys Limited
We can divide the employees in the following teams
-> [0, 1, 2], [1, 0]
7. MEDIUM
You are given a graph with n nodes. Initially, the
graph is disconnected, meaning it contains zero
expert value of team [0, 1, 2] = 3 edges.
expert value of team [1, 0] = 2
Each node has a value written on it such that the ith
So, expert number = 3+2 = 5 node has a value i.
Please note that there is no other way to divide the We say that a range [l, r] is covered in a set s, if for
employees such that expert number is greater than each i from l to r, i appears in s.
5.
Now, let’s define beauty(s), as the minimum
Hence, the maximum value of expert number is 5. number of covered ranges, such that each element
of s belongs to at least one of these ranges. For
Case 3 example: beauty([1, 2, 4, 5, 8, 11]) = 4 (covered
Input: ranges are [1, 2], [4, 5], [8] and [11]).
10
0 You have to process q queries that are either of the
1 following types:
0 • Type 1 - (1, i, j): Add an edge between i and j.
1 • Type 2 - (2, u, 0): Find the number of covered
1 ranges in the connected component of u.
0 Find the sum of answers to all type 2 queries.
3 Sample Input 1
2 2
1 1
0 3
Output: 210
10
Explanation: Sample Output 1
Here, N = 10 1
A = [0, 1, 0, 1, 1, 0, 3, 2, 1, 0]
Sample Output Description 1
We can divide the employees in the following teams Here, n = 2, q = 1, t = 3
-> [0, 1], [0, 1], [1, 0, 3, 2], [1, 0] queries = [[2, 1, 0]]
expert value of team [0, 1] = 2 for query1 -> As there are no edges in the graph,
expert value of team [1, 0, 3, 2] = 4 the connected component of 1 contain only node 1.
only one covered range required i.e. [1, 1]
So, expert number = 2 + 2 + 4 + 2 = 10 Hence, answer for this query is 1.
Please note that there is no other way to divide the So, the answer is 1.
employees such that expert number is greater than
10. Sample Input 2
2
Hence, the maximum value of expert number is 10. 3
3
210
External Document © 2025 Infosys Limited
112 Hence, answer for this query is 2.
210 So, the answer is 2 + 2 = 4.
Sample Output Description 3 for query1 -> As there are no edges in the graph,
Here, n = 10, q = 3, t = 3 the connected component of 1 contain only node 1.
queries = [[1, 1, 4], [2, 1, 0], [2, 4, 0]] only one covered range required i.e. [1, 1]
Hence, answer for this query is 1.
for query1 -> (1, 1, 4)
Add an edge between 1 and 4 So, the answer is 1.
Case 2
table to play a game. Input:
5
The game involves jumping from one chair to 2
another. Each person sitting on chair i can jump A[i] 4
chairs to either the right or left in one jump where 0 5
< i < N+1. 4
3
Bob, sitting on chair X, needs to reach chair Y, where 2
the escape door is located. 1
Case 1
Input:
5
5
1
1
2
3
2
4
Output:
Case 1
10. MEDIUM
You are given an array A of size N.
Input:
1 You can partition A into multiple subarrays such
1 that each element belongs to exactly one subarray
199252 and each subarray has a length of at least K.
470888
Output: The beauty of a subarray is the maximum bitwise
542964004 XOR of the values of a subset in that subarray. The
Explanation: amazingness of a partitioned array is the sum of
Given N = 1, M = 1, B = 199252, R = 470888. beauties of its subarrays.
There is only one great chain: B. Find the maximum possible amazingness of A.
So, answer is 1.
Sample Input 2
5
3
1
3
2
5
4
If k >= 1 then, directed edge 1 -> 2 exists, so, we can In this game, there are N soldiers, where N is an
take the path 1 -> 2. even number. There are also N treasure chests, each
Hence, the minimum value of k required is 1. with a bonus value given by the array Bonus. Here,
So, answer is 1. Bonus[i] denotes the bonus value for the ith chest.
1. For the first soldier (power 1): 1. For soldier 1 (power 1):
The next soldier to the right with a power that is The first soldier to the right whose power is a
a multiple of 1 is the second soldier (power 1). multiple of 1 is soldier 2 (power 2).
The maximum bonus in the range [1st soldier, The maximum bonus in the range [1, 2] is
2nd soldier] is 8. max (4, 2) = 4. Add 4 to the total XP.
Add 8 to the total XP.
2. For the second soldier (power 1): 2. For soldier 2 (power 2):
The next soldier to the right with a power that is The first soldier to the right whose power is a
a multiple of 1 is the third soldier (power 2). multiple of 2 is soldier 5 (power 2).
The maximum bonus in the range [2nd soldier, The maximum bonus in the range [2, 5] is
3rd soldier] is 8. max (2, 1, 4, 5) = 5. Add 5 to the total XP.
Add 8 to the total XP.
3. For the third soldier (power 2): 3. For soldier 3 (power 3):
The next soldier to the right with a power that is The first soldier to the right whose power is a
a multiple of 2 is the fourth soldier (power 2). multiple of 3 is soldier 6 (power 3).
The maximum bonus in the range [3rd soldier, The maximum bonus in the range [3, 6] is
4th soldier] is 2. max(1, 4, 5, 9) = 9. Add 9 to the total XP.
Add 2 to the total XP.
4. The fourth soldier (power 2) has no subsequent 4. For soldier 4 (power 1):
soldiers to the right with a power that is a There are no soldiers to the right whose power is a
multiple of 2, so no bonus is added. multiple of 1. No bonus is added.
Total XP = 8 (from soldier 1) + 8 (from soldier 2) +
2 (from soldier 3) = 18 5. For soldier 5 (power 2):
Therefore, the total XP that Bob can get from N There are no soldiers to the right whose power is a
rounds is 18. multiple of 2. No bonus is added.
Update K as: The answers for the 1st, 2nd, 3rd, and 4th query’s
K = Result = 1. are equal to 1, 2, 2, and 1 respectively.
Second Query (1, 2): Hence, the sum of answers to all queries is equal to
Calculate new values of U and V: 6.
U = (1 + K) % N + 1 = (1 + 1) % 2 + 1 = 1
V = (2 + K) % N + 1 = (2 + 1) % 2 + 1 = 2
© 2025 Infosys Limited, Bengaluru, India. All Rights Reserved. Infosys believes the information in this document is accurate as of its publication date; such information is subject to change without notice. Infosys
acknowledges the proprietary rights of other companies to the trademarks, product names and such other intellectual property rights mentioned in this document. Except as expressly permitted, neither this
documentation nor any part of it may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, printing, photocopying, recording or otherwise, without the
prior permission of Infosys Limited and/ or any named intellectual property rights holders under this document.