back

Visual Binary Search

I volunteer for an organization called Manara, which helps CS graduates (primarily women) in the Middle East get high-paying jobs at big tech companies. I'm doing some question and answer sessions on common types of technical interview problems. In one of the recent sessions we covered some binary search questions. I find it useful to visualize these problems when I'm solving them myself, so I built something that shows the input data and solution code. The goal is to help the viewer understand how the solution algorithm works.

Peak Index in a Mountain Array

A mountain array is one where the values increase for some first portion of the array, and are decreasing in the second portion. The goal of this problem is to find the maximum value, or "peak", of a mountain array.

See the Leetcode question here.

Input Array

Solution Code

> def find_bitonic_max(arr):
    start = 0
    end = len(arr) - 1
    while start < end:
      mid = start + (end - start) // 2
      if arr[mid] > arr[mid + 1]:
        end = mid
      else:
        start = mid + 1
    return arr[start]

Array Visualization

1021324354453627

Start:

Mid:

End: