Amazon Interview Question

Coding: a variation of the Island question from LC