Skip to content
Tactiverge

○ Electronics · binary search

Binary search a resistor series

Find a value in the sorted E12 resistor series by halving the search range, and count the probes.

  • Level 3 · Innovator
  • 5 tests
  • technically checked

Your task

Standard resistors come in the E12 series: 10, 12, 15, 18, 22, 27, 33, 39, 47, 56, 68, 82 (times a power of ten). The list is sorted, so you can find a value by repeatedly checking the middle.

Write findValue(values, target) returning { index, probes }: index is the position of target or −1 if absent, and probes counts how many middle elements you looked at. Use exactly: lo = 0, hi = n − 1; while lo ≤ hi: mid = Math.floor((lo + hi) / 2), probes + 1, then compare.

Run the starter to watch the search: probe(arr, i, lo, hi) records each look.

Hints (0 of 2 shown)

    What gets checked

    • findValue([10, 12, 15, 18, 22, 27, 33, 39, 47, 56, 68, 82], 47) → { index: 8, probes: 2 }
    • findValue([10, 12, 15, 18, 22, 27, 33, 39, 47, 56, 68, 82], 10) → { index: 0, probes: 3 }

    Plus 3 hidden tests for edge cases. They run in the sandbox like the others; only the result is shown.

    Tab indents · Esc then Tab leaves the editor · Ctrl/⌘ Enter runs · Ctrl/⌘ Shift Enter checks · Ctrl/⌘ / comments

    Starting the sandbox…

    Press Run to see what your program prints.

    Your code runs only in this browser, inside an isolated sandbox with no network access, and stops after 3 s. Nothing you type is sent to Tactiverge; drafts and progress are saved in this browser only.