Interview Questions/Coding/Queue Swap Simulation

Queue Swap Simulation

Preview mode. Log in to edit, run, submit, and save progress.

Easy

Queue Swap Simulation

A corridor queue contains B and G markers. During each second, every current occurrence of BG swaps to GB simultaneously. A marker can participate in at most one swap during the same second because all swaps are based on the queue state at the start of that second. Given the initial queue and a number of seconds, report the final queue. Input Format: The first line contains t, the number of test cases. Each test case contains n and seconds, followed by a queue string of length n. Output Format: For each test case, print the queue after all seconds are simulated.

Examples

Example 1
Input:
1
5 1
BGGBG
Output:
GBGGB

Explanation: At the first second, the BG pairs visible in the initial queue swap simultaneously, producing GBGGB.

Approach hint

Start with the simplest clear approach, explain the trade-off, then move toward the cleaner answer.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp