javac SearchingPatterns.java && ./run --mode=interview-prep
Binary Search Patterns
Master binary search with step-by-step visualizations of real LeetCode-style problems. Watch left/mid/right pointers move like debugger variables — understand how the search space halves at each iteration.
01_binary_search.java
LC 704 — Binary Search. Sorted array mein target dhundo, har step mein search space aadha ho jaata hai. Target = 2. Ye sabse basic pattern hai — baaki sab isi se banaye gaye hain.
General Definition
A way to find a value in a sorted list by repeatedly checking the middle element and throwing away the half that can't contain the answer, instead of checking every element one by one.
Professional Definition — say this in an interview
Binary Search is a divide-and-conquer search algorithm that operates on a sorted, monotonic search space. At each step it compares the target with the middle element and eliminates one half of the remaining space, giving O(log n) time complexity and O(1) space for the iterative form — a significant improvement over O(n) linear search.
Interview keywords — if you hear these, think of this pattern
sorted arrayfind the index offind target elementO(log n) time complexitybetter than linear search
SOURCE
Java
public int binarySearch(int[] arr, int target) { int left = 0, right = arr.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) left = mid + 1; else right = mid - 1; } return -1; // not found }
DEBUGGER — live watch0 / 3
2
0
5
1
8
2
12
3
16
4
23
5
38
6
45
7
56
8
72
9
91
10
Not started
DEBUG STEPS0
Press ▶ Play or Next to start stepping through the algorithm.
Practice these — same pattern, different skin