Specialist
Programmer-2024
SAMPLE QUESTIONS
Sample-1: Easy
You are given a binary string S.
Explanation-1
You can perform the following operations on S any number of Here, S = "0000111111" We cannot perform any operation on S so it
times (possibly zero): remains unchanged
• Select an index i such that S[i] is equal to
Sample Input 2
1 and S[i+1] is equal to 0 for 0 ≤ i < len(S) -1. 1111111
• Remove exactly one of the character from S. Sample output 2
1111111
Find the smallest string S that you can get after performing Explanation 2
Here, S = "1111111" There exists no 0 in S so we
operations on S. can not perform any operation on S. Hence, S is
equal to "1111111".
Note: 37
• If there are multiple smallest strings possible then return the
string which is lexicographically smallest. Sample Input 3
110
Constraints
Sample output 3
• 1 <= len(s) <= 10^5 0
• 1 <= 𝐴[𝑖] <= 10^5 Explanation 3
Here, S = "110" We can perform two operations on S as :- 1. Select the
index 1 and 2 and remove the character at index 1. Then S becomes
Sample Input 1 "10". 2. Select the index 0 and 1 and remove the character at index 0.
Then S becomes "0". Hence, S after performing operations is equal to
0000111111 "0"
Sample output 1
0000111111
External Document © 2024 Infosys Limited
Sample 2: Medium
Sample output-2:
You are given a tree consisting of N nodes.
3
Explanation-2:
You are also given two arrays A and P of size N each, where the
value A[i] denotes the value written on the ith node and the Given N = 4, A = [1, 2, 3, 4], P = [0, 1, 2, 3] All of edges 1 - 2, 3 - 2, 3 - 4 are
value P[i] denotes that there is an edge between the good edges. Hence, the answer is equal to 3.
node i and P[i]. Sample Input-3:
6112323012345
We say that an edge is considered good, if after deleting this
Sample output-3:
edge (this will result in formation of 2 trees), the values in each of
the nodes of the trees are distinct. 0
Explanation-3:
Find the total number of good edges present in tree. Given N = 6, A = [1, 1, 2, 3, 2, 3], P = [0, 1, 2,
3, 4, 5] None of the edges are good. Hence, the
Input Format answer is equal to 0.
1. The first line contains an integer, N, denoting the number of
elements in A.
2. Each line i of the N subsequent lines (where 0 ≤ i < N)
contains an integer describing A[i].
3. Each line i of the N subsequent lines (where 0 ≤ i < N)
contains an integer describing P[i].
Constraints
• 1 <= 𝑁 <= 10^5
• 1 <= 𝐴[𝑖] <= 10^5
• 1 <= 𝑃[𝑖] <= 10^5
Sample Input-1:
21101
Sample output-1:
Explanation-1:
Given N = 2, A = [1, 1], P = [0, 1] The only
edge between the node 1 - 2 is good. Hence,
the answer is equal to 1.
Sample Input-2:
412340123
External Document © 2024 Infosys Limited
13
Sample 3 : Hard
Sample Explanation - 1:
You are given 2 arrays A and B, each of size N.
Given N = 5, A = [5, 5, 1, 4, 2], B = [5, 4, 1, 5,
You are now standing at index N and want to move to index 1 by 1]. We can perform the following operations as: 1.
In first move we can move from index 5 to index 2
performing the following operation one or more times until you with cost 3. 2. In second move we can move from
reach index 1: index 2 to index 1 with cost 10. Hence, total cost
is equal to 3 + 10 = 13
Let's say that the element you are currently standing on is i.
Choose index j such that 1 ≤ j < i. Sample input-2:
For each element between i and j, the cost of passing through it 6
5
will be B[j] and the cost of standing at index j will be A[j].
4
2
4
Find the minimum cost to move from index N to index 1. Since, 4
1
the answer can be very large return it modulo 109+7.
1
Input Format 1
4
1. The first line contains an integer, N, denoting the number of 3
3
elements in A and B.
4
2. Each line i of the N subsequent lines (where 1 ≤ i ≤ N)
contains an integer describing A[i].
Sample output-2:
3. Each line i of the N subsequent lines (where 1 ≤ i ≤ N)
contains an integer describing B[i]. 10
Constraints Sample Explanation - 2:
• 2 <= 𝑁 <= 10^5 Given N = 6, A = [5, 4, 2, 4, 4, 1], B = [1, 1, 4, 3, 3, 4]. By performing only
one move we can directly move from index 6 to index 1 with a minimum
• 1 <= 𝐴[𝑖] <= 10^9 cost of 10. Hence, the minimum cost is equal to 10.
• 1 <= B[𝑖] <= 10^9 Sample Input-3:
2
1
Sample Input-1: 1
1
5 1
5
5
1
4
2 Sample output-3:
5 2
4
1 Sample Explanation - 3:
5
1 Given N = 2, A = [1, 1], B = [1, 1]. We can
perform the following operations as: 1. In first
move we can move from index 2 to index 1 with a
Sample output-1: cost of 2. Hence, the minimum cost is equal to 2.
© 2024 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.
Infosys.com | NYSE: INFY Stay Connected