I jumped straight to 'just use a list and sort on read' and the interviewer immediately pushed back.
Clarify the requirements and constraints first, then propose a data structure that maintains sorted order on insertion (e.g., balanced BST or skip list) to avoid re-sorting on retrieval. Discuss trade-offs between insertion and retrieval efficiency, and consider whether a simpler approach like a sorted list with binary search insertion is acceptable given expected data volume.
Pro tip: Mention that you would use a composite key (timestamp, total value) for ordering and discuss how to handle ties or duplicate timestamps. Also, bring up the possibility of using a database with an index if persistence is required, showing awareness of real-world systems.
Ask about expected data volume, frequency of inserts vs. retrievals, and whether persistence is needed. Confirm that sorting is by timestamp ascending, then by total trade value (price * quantity) ascending or descending.
Propose a balanced binary search tree (e.g., Red-Black Tree) or a skip list to maintain sorted order by the composite key. Alternatively, suggest a sorted list with binary search insertion if the dataset is small.
Compare time complexities: O(log n) insertion and O(n) retrieval for BST (in-order traversal), versus O(n) insertion and O(1) retrieval for unsorted list with sorting on demand. Discuss space overhead.
Address duplicate timestamps, negative quantities (if allowed), and how to break ties (e.g., by symbol or insertion order). Consider thread-safety if concurrent access is possible.
Mention how to support additional queries (e.g., by symbol) using secondary indexes, or how to persist trades in a database with appropriate indexing.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.