Samsung Electronics interview question

Tested graphs and algorithms background