Code RoomStable multi-column sort
MediumPrep Room Coding #4881

Stable multi-column sort

CodingAlgorithms & data structuresEntry–Senior~25 min

A spreadsheet view keeps its rows in one list. Each row is a record whose fields are separated by a vertical bar, and every row carries the same number of fields. A reader sorts the view by clicking column headers, one click at a time, and the view re-sorts on each click without forgetting what the earlier clicks did: rows that tie on the clicked column stay in the order they were in when that click landed. clicks holds the column index of each click in the order the clicks happened, and descending says whether that click asked for the reverse direction. Fields compare as plain text, character by character. Return the rows in the order the view shows after the last click. Every click index is valid, the two click lists have the same length, and an empty click list leaves the rows untouched.

Implement
apply_column_clicks(rows: list[str], clicks: list[int], descending: list[bool]) → list[str]
Examples
in[["ada|red|7","bo|blue|3","cy|red|4","di|blue|9"],[1],[false]]out["bo|blue|3","di|blue|9","ada|red|7","cy|red|4"]
in[["ada|red|7","bo|blue|3","cy|red|4","di|blue|9"],[1],[true]]out["ada|red|7","cy|red|4","bo|blue|3","di|blue|9"]
in[["ada|red|7","bo|blue|3","cy|red|4","di|blue|9"],[2,1],[false,false]]out["bo|blue|3","di|blue|9","cy|red|4","ada|red|7"]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 25 min
InputExpectedGot
[["ada|red|7","bo|blue|3","cy|red|4","di|blue|9"],[1],[false]]["bo|blue|3","di|blue|9","ada|red|7","cy|red|4"]not run yetsample
[["ada|red|7","bo|blue|3","cy|red|4","di|blue|9"],[1],[true]]["ada|red|7","cy|red|4","bo|blue|3","di|blue|9"]not run yetsample
[["ada|red|7","bo|blue|3","cy|red|4","di|blue|9"],[2,1],[false,false]]["bo|blue|3","di|blue|9","cy|red|4","ada|red|7"]not run yetsample