Code Room
Code reviewMediumcr-g043
Subject Off by oneLevel Mid–Senior~25 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

Review this Java binary search. It is supposed to return the index of `target` or -1.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix — specific and kind.

Talk through your review
Code to reviewjava
static int search(int[] a, int target) {    int lo = 0, hi = a.length;    while (lo < hi) {        int mid = (lo + hi) / 2;        if (a[mid] == target) {            return mid;        } else if (a[mid] < target) {            lo = mid;        } else {            hi = mid;        }    }    return -1;}
Run or narrate your approach, then ask the coach.