KatInterview

Simulate a Linked List with Iterator and Transform

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designlinked-listiterator-patternhigher-order-functions

Last updated

Tests ability to implement a doubly-linked list supporting sequential iteration and non-mutating element-wise transformation via a command-driven simulation.

Linked List Command Simulator

You are given a sequence of commands to execute against a stateful doubly-linked list. Implement a function that processes each command in order and returns the list of results.

The Data Structure

Your simulation manages one doubly-linked list and one iterator at a time. Each command either modifies the list, creates an iterator, or queries the iterator.

Commands

CommandArgumentsBehaviorReturn value
"append"[val]Add val to the end of the listNone
"map"[op, operand]Replace the current list with a new one where every element has been transformed (see below). The original list is discarded.The new list as a Python list
"iterator"[]Create an iterator positioned at the first element of the current listNone
"hasNext"[]Return True if the iterator has at least one more element, False otherwisebool
"next"[]Return the current element and advance the iterator. If there is no current element, return Noneint or None

Map Operations

The "map" command accepts an operation name and an integer operand:

  • "add" — add operand to each element
  • "multiply" — multiply each element by operand

"map" always produces a new list and does not alter the prior list's nodes. After a "map", the active list is the new transformed list. Any existing iterator refers to the old list and is unaffected.

Simulation Rules

  • The list starts empty.
  • If "hasNext" or "next" is called before any "iterator" command, treat it as if the iterator was created over an empty list: "hasNext" returns False and "next" returns None.
  • "map" on an empty list returns an empty Python list [].
  • Chained "map" commands each operate on the result of the previous one.

Constraints

  • 1 ≤ len(operations) == len(arguments) ≤ 500
  • "append" values satisfy −10^6 ≤ val ≤ 10^6
  • "map" operands satisfy −10^3 ≤ operand ≤ 10^3
  • The only map operations used are "add" and "multiply"
  • operations contains only the five command strings defined above

Examples

Example 1

Input: simulate_linked_list(['append', 'append', 'map', 'iterator', 'hasNext', 'next', 'hasNext', 'next', 'hasNext'], [[1], [2], ['multiply', 2], [], [], [], [], [], []])

Output: [None, None, [2, 4], None, True, 2, True, 4, False]

Example 2

Input: simulate_linked_list(['iterator', 'hasNext', 'next', 'next'], [[], [], [], []])

Output: [None, False, None, None]

Example 3

Input: simulate_linked_list(['append', 'append', 'map', 'map'], [[3], [-2], ['add', 5], ['multiply', 2]])

Output: [None, None, [8, 3], [16, 6]]

Hints

Hint 1

Build the doubly-linked list with a Node class that stores prev and next pointers, and keep a tail pointer so append is O(1) without walking the list.

Hint 2

For the iterator, store just a cursor (a reference to the next node to return). hasNext checks whether the cursor is None; next returns the cursor's value and advances the cursor.

Hint 3

map should walk the current list with its own cursor, apply the transformation, and append each result into a brand-new list — never touching the original nodes.