Find the middle of unknown size list
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When list length is unknown, you cannot jump directly to the midpoint. This is common with linked lists and stream-like traversals where counting first can be expensive. The standard solution is a two-pointer scan that finds the middle in one pass and constant extra memory.
Why Unknown Size Changes the Approach
For arrays, midpoint is easy because random access is available and length is known. For singly linked lists:
- Length is not known without traversal.
- Random access is not available.
- Extra passes can be costly on long lists.
A two-pass strategy still works:
- Traverse once to count nodes.
- Traverse again to index
count // 2.
But one-pass logic is usually preferred for performance and simplicity.
Tortoise and Hare Algorithm
Use two pointers:
slowmoves one node per step.fastmoves two nodes per step.
When fast reaches the end, slow is at the middle.
Complexity:
- Time is linear.
- Extra space is constant.
This is optimal for a single traversal requirement.
Even-Length Lists and Definition Choices
For even length, there are two middle nodes. Different systems choose different conventions:
- Return second middle, which is what the basic loop above returns.
- Return first middle by changing loop condition.
First-middle variant:
Choose one behavior and document it in the function contract.
Single-Pass Alternative: Moving Midpoint Counter
Another one-pass method increments a midpoint pointer every second step.
It works but is less clear than tortoise and hare and easier to get wrong on boundary cases.
Applying to Streams and Iterators
For true iterators where elements are consumed once and not linked, exact middle in one pass is impossible without storing data because you do not know where the end is in advance. Options:
- Buffer all items then index midpoint.
- Use reservoir-like approximations if exact middle is not required.
Linked list middle works because each node is revisitable through references while traversing.
Testing Strategy
Cover these cases:
- Empty list.
- One element.
- Odd length.
- Even length.
- Very long list for performance confidence.
Tests should explicitly state which middle is expected for even lengths.
Common Pitfalls
- Assuming array logic applies directly to linked lists. Fix by using pointer-based traversal, not indexing.
- Forgetting to handle empty input. Fix by returning
Noneearly. - Not defining behavior for even-length lists. Fix by choosing first-middle or second-middle and documenting it.
- Advancing
fastwithout checkingfast.next. Fix by using a safe loop condition. - Running two passes in performance-critical paths without need. Fix by adopting one-pass two-pointer logic.
Summary
- Unknown-size linked lists are best handled with the two-pointer method.
- Tortoise and hare finds the middle in one pass and constant space.
- Even-length behavior must be defined as part of API contract.
- One-pass alternatives exist, but clarity favors two pointers.
- Strong boundary tests prevent most implementation errors.

