Add binary-search module: iterative/recursive search, overflow-safe midpoint, lowerBound/upperBound, rotated arrays, search on the answer, 10 interview problems (14 tests, 14 transcripts)
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b3tGdV3Vm51QUZxg2x
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
# binary-search
|
||||
|
||||
Companion code for the ankurm.com article **Binary Search in Java: Iterative, Recursive, Lower/Upper Bound and 10 Interview Problems**
|
||||
(a full rewrite of the 2015 post "Implementing Binary Search in Java", same URL).
|
||||
|
||||
Iterative and recursive search, the overflow-safe midpoint, `lowerBound`/`upperBound`, rotated arrays, "search on the answer" and ten
|
||||
interview problems, each cross-checked against an obviously-correct brute-force version on seeded random inputs. Every figure and console
|
||||
line in the article is quoted from a file in `output/`, which the JUnit tests write while asserting the same numbers. There is no `docs/`
|
||||
folder: the explanations live in the article itself.
|
||||
|
||||
## Versions
|
||||
|
||||
| Component | Version | Verified against |
|
||||
|---|---|---|
|
||||
| JDK | Temurin 25.0.5.1+2 (LTS) | `java -version` |
|
||||
| JUnit | 6.1.3 | `junit-bom` `maven-metadata.xml` on Maven Central (latest GA) |
|
||||
| Maven | 3.9.x | `mvn -version` |
|
||||
|
||||
## Quickstart
|
||||
|
||||
```bash
|
||||
cd binary-search
|
||||
export JAVA_HOME=/path/to/jdk-25 PATH="$JAVA_HOME/bin:$PATH"
|
||||
./scripts/run-all.sh # runs 14 tests and regenerates output/*.txt
|
||||
java src/main/java/com/ankurm/interviewlab/binarysearch/InteractiveDemo.java # the original console program
|
||||
```
|
||||
|
||||
The first Maven run must be online so the plugins land in the local cache.
|
||||
|
||||
## Source layout
|
||||
|
||||
```
|
||||
binary-search/
|
||||
├── pom.xml
|
||||
├── scripts/run-all.sh
|
||||
├── src/main/java/com/ankurm/interviewlab/binarysearch/
|
||||
│ ├── BinarySearch.java iterative, recursive, lowerBound, upperBound, first/last/count
|
||||
│ ├── Overflow.java the (lo + hi) / 2 bug at 2,000,000,000 elements, and two fixes
|
||||
│ ├── RotatedArray.java search, minimum, and the minimum with duplicates
|
||||
│ ├── SearchOnAnswer.java firstTrue template, integer sqrt, Koko bananas, ship packages
|
||||
│ ├── InterviewProblems.java range, peak element, 2D matrix; index of all ten problems
|
||||
│ ├── BruteForce.java slow, obviously-correct references (no shared helpers)
|
||||
│ ├── DryRuns.java instrumented copies that record every step
|
||||
│ ├── Pitfalls.java two plausible-looking wrong versions
|
||||
│ └── InteractiveDemo.java the original 2015 console program
|
||||
├── src/test/java/.../ one test class per output file group
|
||||
└── output/*.txt captured real output
|
||||
```
|
||||
|
||||
## Captured output (`output/`)
|
||||
|
||||
| File | What it shows |
|
||||
|---|---|
|
||||
| `01-dry-run-iterative.txt` | the three searches from the original article, step by step |
|
||||
| `02-recursive-vs-iterative.txt` | identical answers on 2,003 keys; recursion depth 5 / 11 / 21 |
|
||||
| `03-overflow-mid.txt` | `(lo + hi) / 2` wrapping negative and throwing at n = 2,000,000,000 |
|
||||
| `04-lower-upper-bound.txt` | bounds, first/last/count and `Arrays.binarySearch` on duplicates |
|
||||
| `05-rotated-array.txt` | rotated search and minimum traces; the duplicates degradation |
|
||||
| `06-search-on-answer.txt` | how many yes/no questions sqrt, Koko and ship packages ask |
|
||||
| `07-ten-problems.txt` | one example per problem beside the brute-force answer |
|
||||
| `08-random-agreement.txt` | 34,000 seeded comparisons, 0 disagreements |
|
||||
| `09-operation-counts.txt` | elements looked at: linear vs binary, n = 10 to 1,000,000,000 |
|
||||
| `10-pitfalls.txt` | the classic mistakes, each reproduced |
|
||||
| `11-interactive-session.txt` | the original program, same sample session as the 2015 post |
|
||||
| `12-jdk-source.txt` | the JDK's own midpoint line, read from `lib/src.zip` |
|
||||
| `13-sixteen-elements.txt` | 26 among 16 values: four probes, against 13 for a linear scan |
|
||||
| `14-no-bound-methods.txt` | reflection check: no lowerBound/upperBound in Arrays, Collections or List |
|
||||
Reference in New Issue
Block a user