KatInterview

Ordered Stage Queue: Simulate Item Promotion and Demotion

Linked ListsAuto-graded · 16 test cases
MediumAuto-gradedLinked Listslinked-listsimulationdesign

Last updated

Tests whether a candidate can model item movement across ordered stages using a normalized O(1)-per-move doubly linked list structure.

Problem

You are managing a pipeline of work items organized into an ordered sequence of stages. Each item belongs to exactly one stage at any given time. Items can be promoted to the next stage or demoted to the previous one, and each such transition appends the item to the bottom of the destination stage's list.

Given the initial stage definitions, the initial placement of items, and a sequence of transition instructions, return the final ordering of items within each stage.

Input Format

initial_columns — a list of stage definition objects, each with:

  • "id": a unique string identifier for the stage
  • "title": a human-readable label

Stages are ordered left to right by their position in this list.

initial_tasks — a list of item objects, each with:

  • "id": a unique string identifier
  • "title": a human-readable label
  • "columnId": the "id" of the stage this item currently belongs to

Items are given in their initial top-to-bottom display order within each stage.

moves — a list of [item_id, direction] pairs, where direction is either "left" (move to the preceding stage) or "right" (move to the following stage).

Output Format

Return a list of lists of item ID strings. The outer list has one entry per stage, in the same left-to-right order as initial_columns. Each inner list contains the IDs of items currently in that stage, ordered top to bottom.

Rules

  • When an item moves, it is appended to the bottom of the destination stage.
  • A transition that would move an item beyond the leftmost or rightmost stage is a no-op — the item stays where it is and its position within the stage is unchanged.
  • Transitions are applied strictly in sequence; each one reads the state produced by all prior transitions.
  • If an item is moved right and then left, it lands at the bottom of its original stage, not its original position.

Constraints

  • 1 ≤ len(initial_columns) ≤ 100
  • 0 ≤ len(initial_tasks) ≤ 10^5
  • 0 ≤ len(moves) ≤ 10^5
  • All stage "id" values are unique strings.
  • All item "id" values are unique strings.
  • Every item's "columnId" refers to a valid stage "id".
  • Every transition's item_id refers to a valid item "id".
  • direction is always "left" or "right".
  • Each move of a single item must run in O(1) time with respect to the total number of items.

Examples

Example 1

Input: simulate_kanban_moves([{'id': 'todo', 'title': 'To Do'}, {'id': 'in_progress', 'title': 'In Progress'}, {'id': 'done', 'title': 'Done'}], [{'id': '1', 'title': 'Setup Repo', 'columnId': 'todo'}, {'id': '2', 'title': 'Write Tests', 'columnId': 'todo'}, {'id': '3', 'title': 'Deploy', 'columnId': 'in_progress'}], [['1', 'right']])

Output: [['2'], ['3', '1'], []]

Hints

Hint 1

Track which stage each item currently lives in using a dictionary so you never need to scan all items to locate one.

Hint 2

Removing an item from the middle of a list is expensive unless each item knows its immediate neighbors — think about what data structure gives you O(1) unlinking.

Hint 3

Appending to the bottom of a stage only requires updating that stage's tail pointer; keep a head and tail pointer per stage to make both removal and insertion O(1).