Navigate Previous and Next Browser Tabs
Updated
Problem statement
A browser has a fixed ordered array of open tabIds. The active tab initially has zero-based index startIndex. Process the commands in order and return the active tab ID after each command.
For this exercise, PREVIOUS moves one position left if a tab exists there, and NEXT moves one position right if one exists there. At either boundary, an action that would move beyond the array leaves the active tab unchanged. There is no wraparound.
Tab IDs are unique labels, not array indices or a sorting order. The array order never changes; no tabs are opened or closed. This task concerns neighboring browser tabs, with no page-history stack. Do not emit the initial tab before processing commands. An empty command sequence returns an empty array. Leave all inputs unchanged.
Function
navigateTabs(tabIds: int[], startIndex: int, commands: String[]) → int[]Examples
Example 1
tabIds = [11,22,33]startIndex = 1commands = ["NEXT","NEXT","PREVIOUS","PREVIOUS","PREVIOUS","NEXT"]return = [33,33,22,11,11,22]Start on ID 22. NEXT reaches 33 and the second NEXT stays 33 at the last boundary. Three PREVIOUS actions give 22,11,11, then NEXT gives 22. Emit only the six post-action IDs.
Example 2
tabIds = [7]startIndex = 0commands = ["PREVIOUS","NEXT"]return = [7,7]Only ID 7 is open. Neither direction has a neighbor, so both commands leave 7 active and return [7,7].
Constraints
- For this exercise, assume 1 <= tabIds.length <= 500.
- Every tab ID is between 1 and 1000000, inclusive.
- All tab IDs are distinct.
- For this exercise, assume 0 <= startIndex <= 499.
- startIndex is less than tabIds.length.
- For this exercise, assume 0 <= commands.length <= 2000.
- Every command string has between 4 and 8 characters, inclusive.
- Every command is exactly PREVIOUS or NEXT, in uppercase.
More Morgan Stanley Onsite Interview questions
- Generate Strings of Length at Least ThreeONSITE INTERVIEW · Seen Sep 2026
- Maximum Concurrent ProcessesOA · ONSITE INTERVIEW · Seen Sep 2026
- Kth Largest Array ElementONSITE INTERVIEW · Seen Jul 2026
- Run-Length Encode Consecutive Letter RunsONSITE INTERVIEW · Seen Jul 2026
- Maximum Edge Removals for Two TravelersONSITE INTERVIEW · Seen Oct 2025
- Remove K DigitsONSITE INTERVIEW · Seen Oct 2025
- Count Ideal ArraysONSITE INTERVIEW · Seen Aug 2023
- Distinct Palindrome PartitionsONSITE INTERVIEW · Seen Aug 2023
- LFU Cache OperationsONSITE INTERVIEW · Seen Aug 2023
- All-Pairs Shortest PathsOA · ONSITE INTERVIEW · Seen Feb 2022
Similar questions at other companies
- Ring Buffer OperationsAmazon · ONSITE INTERVIEW
- Count Distinct Underwriting PII ValuesAffirm · PHONE SCREEN · ONSITE INTERVIEW
- Bounded Word Guess SolverAtlassian · ONSITE INTERVIEW
- Generate an Unbiased Bit from a Biased CoinGoogle · ONSITE INTERVIEW
- Apply Overridden Integer TransformsOptimus Information · ONSITE INTERVIEW
- Copy File Bytes with Partial WritesMicrosoft · ONSITE INTERVIEW
- Policy Violation Set ChecksPinterest · ONSITE INTERVIEW
- Retry with an Attempt LimitOracle · ONSITE INTERVIEW
- Extract Error Log ContextFlexTrade · ONSITE INTERVIEW
- Moving Cost by Volume and CategoryWayfair · ONSITE INTERVIEW