Visa Inc. interview question

Coding question on graphs and difficulty is medium