Samsung Electronics Interview Question

Something to do with backtracking and optimization