Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Shashwat Kumar
Wont work if we dont return the edge immediately?

Shashwat Kumar

Sep 3, 2024

The problem clearly states, 'If there are multiple answers, return the edge that occurs last in the input.' but the solution returns the edge immediately. If we try to instead overwrite the res array and return it after the for loop it gives memory error in C++;

1

0

Comments
Comments
Divyanshu Varma
Divyanshu Varma6 months ago

Correct, if we use the last edge that can be removed we get a memory error. Turns out the test case [[1,2],[2,3],[3,1],[4,5],[5,6]] fails. If you see, there are two components which means there can never be an edge upon removal of which the given graph makes a tree. How...