Grokking Graph Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
I don't think this algorithm is correct, at least it doesn't pass tests when I t...

Jonathan

Sep 6, 2022

I don't think this algorithm is correct, at least it doesn't pass tests when I try it this way and doesn't match Kahn's as I understand it. I think this should be:

graph.get(child).add(parent); // put the parent into it's child's list inDegree.put(parent, inDegree.get(parent) + 1); // increment parent's inDegree

...and likewise on the subsequent problems.

3

1

Comments
Comments
Design Gurus
Design Gurus3 years ago

Can you share an input where this solution fails for you?

J
J 3 years ago

@Jonathan is correct regarding the first two test cases -- { numCourses = 2, prerequisites = [[1,0]] } and { numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]] } for LC 210. Course Schedule II [https://leetcode.com/problems/course-schedule-ii/description/](https:...

J
J 3 years ago

Forgot to mention this in my earlier comment but basically the way the directed edges is defined on LeetCode is reversed from the definition here in Grokking; in Grokking the directed edge pair is [prereq, task] while on LeetCode it is [task, prereq].

On this page