Range lookup in Java
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Range lookup means mapping a value into the interval that contains it. Typical examples are tax brackets, grade bands, pricing tiers, and risk levels. In Java, the best implementation depends on how many ranges you have, whether they change at runtime, and whether lookups happen much more often than updates.
Start With A Clear Range Model
Before choosing a data structure, define the rules precisely:
- are range boundaries inclusive or exclusive,
- can ranges overlap,
- are the ranges sorted,
- what happens if no range matches.
Those rules matter more than the syntax because they determine whether the lookup logic is correct.
Simple Sequential Lookup
For a small fixed list of ranges, a linear scan is often perfectly fine and easier to maintain than a more complex structure.
If there are only five or ten ranges, this is often the most reasonable solution.
Binary Search On Sorted Boundaries
If the ranges are sorted and lookups are frequent, binary search reduces lookup cost.
This works only if the ranges are non-overlapping and sorted by start value. If those assumptions are false, binary search will produce unreliable results.
TreeMap Is Useful For Dynamic Thresholds
When the lookup is based on threshold starts rather than full range objects, TreeMap offers a clean approach with floorEntry.
This style is ideal when each threshold means "from here upward until the next threshold." It is common in grading and pricing problems.
Validate Ranges Up Front
A robust solution checks for overlaps and invalid input during construction instead of letting bad ranges silently produce wrong answers later.
That kind of guardrail often matters more than micro-optimizing the lookup itself.
Which Approach To Choose
Use a simple scan when:
- the range list is small,
- clarity matters more than asymptotic improvement,
- the ranges rarely change.
Use binary search when:
- the ranges are sorted and non-overlapping,
- lookups are frequent,
- you want predictable fast lookup in a static list.
Use TreeMap when:
- thresholds can change dynamically,
- floor-based lookup expresses the business rule naturally,
- you want built-in ordered navigation operations.
Common Pitfalls
- Failing to define whether boundaries are inclusive or exclusive.
- Applying binary search to unsorted or overlapping ranges.
- Using a complex structure when a short linear scan would be simpler and sufficient.
- Forgetting to handle values outside every defined range.
- Storing only labels without validating that the thresholds actually describe valid intervals.
Summary
- Range lookup is mostly about precise interval rules, not just data structures.
- A linear scan is often enough for small, fixed rule sets.
- Binary search is a good fit for sorted, non-overlapping ranges.
- '
TreeMap.floorEntryis elegant for threshold-based mappings.' - Validate ranges early so bad configuration does not turn into silent wrong answers.
Related reading
- Re-run Spring Boot Configuration Annotation Processor to update generated metadata
- ReactiveSecurityContextHolder is empty in Spring WebFlux
- Read-only filesystem pod with Spring Boot application on Kubernetes
- Read environment variable in SpringBoot
- Read file from resources folder in Spring Boot
- Read file from resources folder in Spring Boot
- Read resource text file to String in Java
- Read URL to String in few lines of Java code

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.