TCS NQT Programming Revision Sheet
Practice Problems Covered
1. Move Zeros to End of Array
Idea: Traverse array, move non-zero elements forward, fill remaining with 0.
Example:
Input: 1 0 3 0 5
Output: 1 3 5 0 0
2. Move Zeros to Beginning
Idea: Traverse from right side, place non-zero elements at the end, fill beginning with 0.
Example:
Input: 1 0 3 0 5
Output: 0 0 1 3 5
3. Two-Wheeler & Four-Wheeler Production
Given total vehicles (V) and total wheels (W).
Equations:
x + y = V
2x + 4y = W
Solution:
four = (W - 2V) / 2
two = V - four
4. Count Sundays in a Month
Input:
Total days in month (d)
Starting day of month (s)
First Sunday = 8 - s
Then count every 7 days.
5. Chocolate Distribution Problem
Given:
N chocolates, K students, starting student S.
Formula:
last = (S + N - 1) % K
If last == 0 → answer = K
6. Leaders in an Array
An element is leader if it is greater than all elements to its right.
Example:
Array: 16 17 4 3 5 2
Leaders: 17 5 2
7. Rotate Array Right K Times
For each rotation:
1. Save last element
2. Shift all elements right
3. Place last element at index 0
Example:
1 2 3 4 5 6 → rotate 2 → 5 6 1 2 3 4
8. Equilibrium Index
Index where:
sum(left elements) = sum(right elements)
Example:
1 3 5 2 2
Equilibrium index = 2
9. Missing Number in Array
Numbers from 1..n with one missing.
Formula:
expected_sum = n(n+1)/2
missing = expected_sum - actual_sum
10. Autobiographical Number
Digit at index i represents how many times digit i appears.
Example:
1210
Index 0 → one 0
Index 1 → two 1s
Index 2 → one 2
Index 3 → zero 3s
11. Maximum Subarray Sum (Kadane’s Algorithm)
Find maximum sum of a contiguous subarray.
Idea:
current_sum += element
if current_sum < 0 → reset to 0
track max_sum