Find ID of Soldier
Reported by candidates from HSBC's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks most first attempts at this HSBC question, reported in June 2026, is building the array of N soldiers. N goes up to 10^9, so you'll blow memory before you swap anything. This is really a range-reversal problem in disguise, and you only need to track one position. If you blank on the trick during the live assessment, StealthCoder runs invisibly on your desktop as a safety net and hands you the approach. But the idea is short enough to hold in your head tonight.
The problem
There are N soldiers standing in a line, with IDs from 1 to N, in ascending order. They are participating in an exercise consisting of Q actions. During the ith action, the Major calls S numbers rowi and coli. The soldiers at the rowith and colith positions swap places; then the soldiers at (rowi+1)th and (coli-1)th positions swap places, and so on until (rowi+m) < (coli-m). Each of the soldier's IDs will be covered in the range [rowi, coli] for at most one action. Write an algorithm to find the ID of the soldier at Kth position in the line after all the actions are completed. Input The first line of the input consists of an integer- num, representing the number of soldiers (N). The second line consists of two space-separated integers- actions and numSoldiers, representing the number of actions (Q) and number of soldiers called by the Major (S), respectively. The next Q lines consist of S space-separated integers - rowi and coli, representing the positions of the soldiers initially called for the ith action. The last line consists of an integer- posSoldier, representing the position of the soldier whose ID is requested to be found after Q actions (K). Output Print an integer representing the ID of the Kth position soldier in the line after Q actions. Function findIdOfSoldier(num: int, actions: int, numSoldiers: int, swaps: int[][], posSoldier: int) → int Examples Example 1 num = 10 actions = 2 numSoldiers = 2 swaps = [[1, 5], [6, 10]] posSoldier = 1 return = 5 Step1: After the 1st action, the position of soldiers is in the order: 5 4 3 2 1 6 7 8 9 10. Step2: After the 2nd action, the position of soldiers is in the order: 5 4 3 2 1 10 9 8 7 6. Step3: The ID of the soldier at position 1 is 5. So, the output is 5. Constraints 1 <= posSoldier <= num <= 10^9 1 <= actions <= 10^5 1 <= rowi <= coli <= num 1 <= i <= actions
Reported by candidates. Source: FastPrep
Pattern and pitfall
Each action reverses the segment [row, col]. The statement guarantees every soldier is covered by at most one action, so the ranges never overlap. That's the whole trick. Position K is either outside every range, so the ID is just K, or inside exactly one range [l, r]. Reversing maps position K to l + r - K, and that's the answer. Scan the Q actions once, find the range containing K, and return l + r - K. If none contains it, return K. That's O(Q) time and O(1) extra space. The pitfall is simulating swaps or allocating an array, which fails at 10^9. Another trap is applying the reflection more than once, which is wrong because the ranges are disjoint. Read input carefully, since the S parameter is always 2 in practice. If you freeze under the clock, StealthCoder is the hedge that surfaces this reflection formula during the live OA.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Find ID of Soldier cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass HSBC's OA.
HSBC reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find ID of Soldier FAQ
How hard is the HSBC Find ID of Soldier problem really?+
Easy once you see it, hard if you simulate. The code is about five lines. The difficulty is noticing that N up to 10^9 rules out building the line, and that the disjoint-range guarantee means only one reversal can affect position K.
What's the trick to Find ID of Soldier?+
Reversing a segment [l, r] sends position K to l + r - K. Because each soldier is covered by at most one action, find the single range containing K, apply that formula once, and you're done. If no range contains K, the answer is K itself.
Why can't I just simulate the swaps?+
N can reach 10^9, so an array of that size won't fit in memory. Even with a smart structure, you don't need the full line. You only need where one position ends up, which is a constant-time calculation per action.
What's the time complexity I should aim for?+
O(Q) time and O(1) extra space. You loop through the actions once, check if row <= K <= col, and compute the reflected ID. With Q up to 10^5, that runs instantly. Anything involving N in the complexity is the wrong direction.
How do I prepare for this in 48 hours?+
Practice range-reversal and index-mapping thinking, where you track one position instead of the whole structure. Write the reflection formula l + r - K from memory, test it on the sample (K=1, range [1,5] gives 5), and handle the case where K sits in no range.