Sorting and Searching Questions With a Finance Setting
Sorting and searching interview questions test tie handling in price ladders, percentile lookups and anomaly search, not a plain textbook sort.
Sorting and searching questions in a finance interview rarely ask for a plain sort or a plain binary search. They ask for insertion order preserved at a tied price, for a percentile computed without touching every element, or for a search that has to account for one anomalous value sitting inside a sequence that is otherwise ordered. Four variants recur across firms: locating an insertion point in a price ladder, sorting trades by price then arrival time, finding a percentile of order sizes without a full sort, and searching a session's price series for a single out-of-place tick.
State the problem precisely: given a sorted array of resting price levels in a limit order book, find the index at which a new order's price should be inserted to keep the array sorted, which is the mechanism behind adding a new price level to the book. Binary search solves this in O(log n) by repeatedly halving the search range, and the candidate needs the exact variant that returns the leftmost valid insertion point when the new price matches an existing level, since price levels do not duplicate in a book.
The follow-up almost always asks why an array is the wrong container once insertions and deletions happen thousands of times a second, since insertion into a sorted array costs O(n) to shift the remaining elements even though finding the position only costs O(log n). This is the opening into the structures covered in designing an order book data structure in an interview, where a fixed price band keyed by tick index removes the shifting cost entirely.
State the problem precisely: given a batch of resting orders at the same price, they must be ranked by arrival time to preserve first-in-first-out priority, while the overall book is sorted by price across levels. A stable sort, one that preserves the relative order of elements that compare equal on the primary key, solves the arrival-time tie directly, since sorting first by price with a stable algorithm leaves same-price orders in their original arrival order without a second explicit sort key.
The follow-up asks the candidate to name which common sorting algorithms are stable and which are not, since merge sort and insertion sort are stable while a naive quicksort and heapsort are not, and to explain what breaks in a matching engine if an unstable sort is used by mistake. An order that arrived second could execute ahead of one that arrived first at the same price, which is a fairness problem and, at firms like Citadel Securities, a regulatory exposure.