linear solution for Amazon2ndInterview Question