How Sorted Sets Work — API Cheat Sheet
No exercise here — TreeSet/SortedSet and their nearest equivalents, for when you need unique values kept in sorted order with fast lookups.
This is a reference page, not a graded exercise. A sorted set keeps its elements unique and always in sorted order, with add/remove/contains all faster than re-sorting an array every time (typically O(log n), backed by a balanced tree).
Java — TreeSet<T>
| Want to... | Method | Example |
|---|---|---|
| Create | new TreeSet<>() | TreeSet<Integer> s = new TreeSet<>(); |
| Add / remove / check | .add(v) / .remove(v) / .contains(v) | same names as HashSet |
| Smallest / largest | .first() / .last() | throws if empty |
| Remove-and-return smallest/largest | .pollFirst() / .pollLast() | returns null if empty |
| Nearest neighbors | .floor(v) (≤ v), .ceiling(v) (≥ v), .lower(v) (< v), .higher(v) (> v) | this is the feature a plain HashSet can't give you |
| Sorted key-value version | TreeMap<K, V> | same idea, sorted by key |
C# — SortedSet<T>
| Want to... | Method | Example |
|---|---|---|
| Create | new SortedSet<T>() | var s = new SortedSet<int>(); |
| Add / remove / check | .Add(v) / .Remove(v) / .Contains(v) | |
| Smallest / largest | .Min / .Max | properties |
| Range view | .GetViewBetween(low, high) | returns the subset in [low, high] |
| Sorted key-value version | SortedDictionary<K, V> | same idea, sorted by key |
Python — no balanced-BST built-in
Python's standard library doesn't ship a sorted-set type. Two common workarounds, both fine to mention out loud in an interview:
| Approach | How |
|---|---|
sortedcontainers.SortedList / SortedSet | third-party but extremely common; O(log n) add/remove/contains, same shape as Java's TreeSet. Ask your interviewer if it's available. |
bisect module on a plain sorted list | bisect.insort(lst, v) inserts keeping lst sorted; bisect.bisect_left(lst, v) finds where v would go — O(log n) to find the spot, but O(n) to actually shift the list, so it's not truly O(log n) overall |
heapq | good for "give me the min/max repeatedly," not for "keep everything sorted and query arbitrary ranges" — see the Priority Queue cheat sheet |
JavaScript / TypeScript — no built-in either
Same situation as Python: no native sorted-set/balanced-BST type. In an interview, it's usually fine to either say so explicitly and describe the approach, or keep an array sorted via manual binary-search insertion (O(n) per insert because of the array shift, O(log n) just to find the index).
The gotcha that costs the most time: don't confuse a sorted set with a priority queue/heap — a heap only guarantees the minimum (or maximum) is accessible in O(log n), not that the whole collection is in order. If a problem needs arbitrary range queries ("give me everything between 10 and 20") or predecessor/successor lookups, that's a sorted-set problem; if it only needs repeated access to the current min/max, that's a heap problem and will be faster.
Hints
Companies
No companies reported yet.
Discussion
Sign in to join the discussion.
Loading discussion...
Test results
This question doesn't have a code exercise.