Quick Overview

Build an insertion-ordered container that stores only the latest quote for each stock symbol. Support expected O(1) push, pop, and size operations while updating bid and ask values without changing a symbol's original position and safely rejecting malformed commands.

Build an Ordered Latest-Quote Container

Company: Chicago Trading Company

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

# Build an Ordered Latest-Quote Container Implement a top-level operation runner for incoming stock quotes. At most one quote per symbol may be present. ```python def run_quote_container(operations: list[list]) -> list: ... ``` Operations are encoded as: - `["push", symbol, bid, ask]`: insert or update a quote and append `None` to results. - `["pop"]`: remove the oldest remaining symbol and append its latest `[symbol, bid, ask]`, or append `None` when empty. - `["size"]`: append the current number of symbols. Malformed or unknown operations append the string `"ValueError"` and leave state unchanged. The first valid push of a symbol establishes its position in insertion order. A later valid push for that symbol replaces its bid and ask **without changing that position**. Every valid operation must run in expected `O(1)` time. ### Example ```text Input operations: [ ["push", "AAPL", 95, 97], ["push", "GOOG", 500, 540], ["push", "MSFT", 30, 34], ["push", "GOOG", 501, 520], ["push", "AAPL", 94, 98], ["size"], ["pop"], ["push", "MSFT", 32, 35], ["pop"], ["pop"], ["pop"] ] Output: [ None, None, None, None, None, 3, ["AAPL", 94, 98], None, ["GOOG", 501, 520], ["MSFT", 32, 35], None ] ``` ### Constraints - Symbols are nonempty case-sensitive strings. - Bid and ask are integers. - The container may hold up to `200_000` distinct symbols. - Operation records must have exactly the shapes defined above. ### Clarifications - This practice version follows the example: updating a symbol does not make it newest in the ordering. - After a symbol is popped, a later push of that symbol is a new first arrival and appends it to the back. ### Hints - One requirement concerns order; another requires direct access by symbol. - Choose representations that let a replacement update the exact ordered element without scanning.

Overview: Build an insertion-ordered container that stores only the latest quote for each stock symbol. Support expected O(1) push, pop, and size operations while updating bid and ask values without changing a symbol's original position and safely rejecting malformed commands.

Read the full Chicago Trading Company Software Engineer interview experience this question came from

Maintain at most one latest quote per symbol while retaining each symbol's first-arrival order. Push inserts or replaces without moving an existing symbol; pop removes the oldest symbol; size reports the count. Malformed operations append the literal string ValueError and leave state unchanged.

Constraints

  • Symbols are nonempty case-sensitive strings.
  • Bid and ask are non-Boolean integers.
  • The container may hold at most 200,000 symbols.
  • Valid push, pop, and size operations run in expected O(1) time.

Examples

Input: ([['push','AAPL',95,97],['push','GOOG',500,540],['push','MSFT',30,34],['push','GOOG',501,520],['push','AAPL',94,98],['size'],['pop'],['push','MSFT',32,35],['pop'],['pop'],['pop']],)

Expected Output: [None, None, None, None, None, 3, ['AAPL', 94, 98], None, ['GOOG', 501, 520], ['MSFT', 32, 35], None]

Explanation: The supplied sequence exercises replacement and order.

Input: ([['pop'],['size']],)

Expected Output: [None, 0]

Explanation: Pop on an empty container returns None.

Hints

  1. An ordered dictionary supports direct replacement and oldest-item removal.
  2. After a symbol is popped, a later push is a fresh arrival at the back.

Loading coding console...

Show the approach

Approach

OrderedDict preserves first insertion order while assignment replaces the current quote in place. popitem(last=False) removes the oldest remaining symbol in constant expected time.

Time complexity:
O(number of operations) expected
Space complexity:
O(number of distinct resident symbols)