BNSF Railway Interview Question

It was a medium LC problem