FastPrepFind ID of Soldier
Problems/Find ID of Soldier

Find ID of Soldier

HSBC logoHSBC● EasyOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

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 = 10actions = 2numSoldiers = 2swaps = [[1, 5], [6, 10]]posSoldier = 1return = 5

Step 1: After the 1st action, the position of soldiers is in the order: 5 4 3 2 1 6 7 8 9 10.

Step 2: After the 2nd action, the position of soldiers is in the order: 5 4 3 2 1 10 9 8 7 6.

Step 3: 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

More HSBC Online Assessment questions

  • Organization Reputation After Employee DeparturesOA · Seen Nov 2026
  • Remove Vowels from a StringOA · Seen Nov 2026
  • Minimum Deletion Cost for Distinct Adjacent CharactersOA · Seen Oct 2026
  • Distinct Products After Removing One ElementOA · Seen Sep 2026
  • Minimum Cleaning Robot RunsOA · Seen Sep 2026
  • Minimum Satellite Data Transfer IterationsOA · Seen Aug 2026
  • Find Most Frequently Purchased ProductsOA · Seen Jun 2026
  • Minimum Markers to Clear Line SegmentsOA · Seen Nov 2025
  • Arrange Flower SticksOA · Seen Mar 2025
  • Find Largest House AreaOA · Seen Mar 2025

Similar questions at other companies

  • Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
  • Book Reading EveningsGoogle · OAGoogle interview questions
  • Basic Item DiscountsPalantir · OAPalantir interview questions
  • Wind-Blown Leaves Remaining in a GridAbridge · OAAbridge interview questions
  • Lift Total TravelInfosys · OAInfosys interview questions
  • Shell Command Counts with History ReferencesTikTok · OATikTok interview questions
  • Count Dropped RequestsJP Morgan Chase · OAJP Morgan Chase interview questions
  • Recursively Expanded Shell Command CountsHudson River Trading · OAHudson River Trading interview questions
  • Diagonal Bounce TraversalAutodesk · OAAutodesk interview questions
  • First Value Below Both NeighborsCapital One · OACapital One interview questions
All HSBC interview questionsSee HSBC hiring insights
Saved
class Solution {
    public int findIdOfSoldier(int num, int actions, int numSoldiers, int[][] swaps, int posSoldier) {
      // write your code here
    }

}
Checking account…