Grokking the Coding Interview: Patterns for Coding Questions
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
Design Gurus
Design Gurus3 years ago

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

J
J 3 years ago
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].