From list of integers, get number closest to a given value
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Finding the number closest to a target is a small problem that appears in ranking, recommendations, calibration, and search utilities. The one line solution works for simple cases, but production code also needs deterministic tie rules, empty input handling, and good performance for repeated queries. The right strategy depends on query volume, input size, and whether the list changes between lookups.
One Off Query with min
For a single lookup, min with a key function is concise and correct.
This is linear time because every element is inspected once. For one call on a small list, this is usually best for readability.
Add Deterministic Tie Breaking
If two numbers are equally close, define which should win. Without an explicit rule, behavior may be surprising for users and tests.
A common policy is to choose the smaller value.
If your domain wants the larger value instead, invert the secondary key.
Document this policy in function naming or docstrings so it is not ambiguous.
Handle Empty Lists Explicitly
min on an empty list raises ValueError. Decide whether your API should raise, return None, or provide a fallback.
Returning None is convenient in some pipelines, but raising is safer when absence is a true error condition.
Optimize Repeated Queries with Sorting and bisect
If you need many target lookups on a fixed list, sorting once and using binary search reduces per query cost.
Complexity becomes O(log n) per query after O(n log n) preprocessing.
This optimization only pays off when you reuse the sorted structure. For a single lookup, sorting first is slower than a direct linear scan, because the extra O(n log n) work does not buy you anything.
Return More Than Just the Value
Many use cases need index or distance too. If duplicates exist, index handling should be explicit.
If index is required, iterate with enumerate and include index in your key tuple.
Consider Numeric Type and Range
Even though the title mentions integers, data pipelines often drift to floats or strings from external sources. Validate input type early if strict integer behavior matters. For huge integers, Python handles big values correctly, but absolute differences may still be expensive for very large lists and repeated calls.
For strict APIs:
If the list is already sorted upstream, preserve that fact instead of rebuilding work inside the helper. A small API choice such as accepting a pre sorted collection can matter when the function becomes part of a larger numerical pipeline.
Common Pitfalls
- Ignoring tie breaking and getting nondeterministic or surprising outcomes.
- Using a linear scan for high volume repeated query workloads.
- Forgetting empty list behavior and triggering runtime exceptions.
- Returning only value when callers also need distance or index.
- Skipping input validation when data can contain non integer values.
Summary
- For one query,
minwith absolute difference is simple and effective. - Define explicit tie policy to keep behavior deterministic.
- Handle empty inputs intentionally with clear API semantics.
- For many queries, sort once and use
bisectfor faster lookups. - Return value plus metadata when downstream code needs more context.

