Determine reachability and optimize task scheduling | Amazon Interview Question