Nearest Resting Price Level for SoFi Invest Limit Orders

Last updated: November 29, 2026

Quick Overview

Design a structure that stores resting limit order prices in cents with duplicates, supports add and remove, returns the closest existing price to a target (ties go to the lower price), and returns the best price at or below a target.

SoFi
Coding & Algorithms
Software Engineer
SoFi
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

0

0

0 solved


Design a structure that stores resting limit order prices in cents with duplicates, supports add and remove, returns the closest existing price to a target (ties go to the lower price), and returns the best price at or below a target.

SoFi Invest routes orders and shows members price context, and internal tools often need fast floor and ceiling lookups over price levels. The interviewer checks ordered-set thinking with binary search, multiset counting, and honest discussion of insertion cost in Python versus a balanced tree.

What the Interviewer Expects
  • Keeps a sorted list of distinct prices plus a count map for duplicates
  • Uses bisect_left and bisect_right correctly for nearest and floor queries
  • Applies the lower-price tie-break explicitly
  • Removes the level only when its count reaches zero
  • Notes O(n) insort cost and the balanced BST or SortedList alternative
Key Topics to Cover
binary search
ordered sets
multisets
order books
bisect
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you also return total share quantity at each level?
  • How would you support 'k nearest levels' efficiently?
  • What would you change for a very high update rate with few queries?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Prices are integer cents to avoid float comparison issues. Operations: add(price), remove(price) -> bool, nearest(target), best_at_or_below(target). Return None when empty or no qualifying l...

High-Level Design

Store distinct prices in a sorted list and per-price order counts in a dict. Adding an existing price only bumps the count; removing decrements and deletes the level at zero. For nearest, `bisect_le...


Submit Your Answer
Markdown supported