Find longest segment with dominant ends | Amazon Interview Question