Interview questions · Capital One
Find lexicographically smallest string and elimination order
These problems evaluate string-manipulation and algorithmic-simulation competencies—specifically lexicographic ordering and prefix/suffix transformations for the first problem, and cumulative-time aggregation with elimination and lexicographic tie-breaking for the second.
You remember two coding questions from an online assessment.
Question 1: Return the lexicographically smallest string after a prefix/suffix reverse
You are given a string s (length n). You may perform at most one operation:
- Choose an integer
kwhere1 <= k <= n. - Either:
- Reverse the first
kcharacters ofs(reverse a prefix of lengthk), leaving the rest unchanged, or - Reverse the last
kcharacters ofs(reverse a suffix of lengthk), leaving the rest unchanged.
- Reverse the first
Return the lexicographically (alphabetically) smallest string that can be obtained among:
- doing no operation, and
- doing one valid operation as above.
Output: the smallest resulting string.
Question 2: Simulate race eliminations using drivers’ history across laps
You are given laps, an array of length n representing a multi-lap race.
- Each
laps[i]is an array of pairs[driverName, lapTime]. lapTimeis a positive integer.- A driver may appear in earlier laps and then be eliminated; eliminated drivers no longer appear in subsequent laps.
Maintain each driver’s history so far using cumulative total time across all completed laps. After each lap is processed:
- Update each active driver’s cumulative time by adding their time from this lap.
- Eliminate the slowest active driver, defined as the driver with the largest cumulative total time so far.
- If there is a tie, break ties by
driverNamein ascending lexicographic order.
- If there is a tie, break ties by
Return the elimination order as an array of driver names, from first eliminated to last eliminated.
Output: string[] elimination order.
Notes / assumptions to make the task well-defined:
- Each lap input includes exactly one entry for each currently-active (not-yet-eliminated) driver.
- There is at least 2 drivers at the start.
- Exactly 1 driver is eliminated after each lap until 1 remains (so total eliminations = initialDrivers - 1).
Community answers
Nothing here yet.
Join in on CSWork →