Start by clarifying the problem and edge cases, then implement Kahn's algorithm (BFS in-degree) and DFS-based topological sort, explaining how each detects cycles. Compare their time and space complexities, and discuss handling multiple valid orderings by noting that any valid order is acceptable unless a specific tie-breaking rule is required.
Pro tip: Mention that Kahn's algorithm can detect cycles by checking if the number of processed nodes is less than n, while DFS detects cycles via back edges. Also, note that both approaches have O(n+m) time complexity, but DFS may use more space due to recursion stack.
Confirm whether the graph is guaranteed to be a DAG, if multiple orderings are acceptable, and discuss edge cases like disconnected graphs, self-loops, and empty graphs.
Compute in-degrees, enqueue nodes with zero in-degree, and process them while decrementing in-degrees of neighbors. If the processed count is less than n, a cycle exists.
Perform DFS, marking nodes as unvisited, visiting, or visited. Add nodes to the front of a list upon completion. A back edge to a visiting node indicates a cycle.
Both run in O(n+m) time. Kahn's uses O(n) space for in-degree array and queue; DFS uses O(n) for visited states and recursion stack, which can be O(n) in worst case.
Explain that any valid topological order is acceptable unless a specific order is required. To handle multiple orderings, one can use a priority queue (e.g., min-heap) to always pick the smallest available node, producing a lexicographically smallest order.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Sliding window with a fixed-size frequency array, pretty standard anagram-finding problem.
Use a sliding window of length equal to the pattern, maintaining character frequency counts for the window and the pattern. Compare the frequency maps at each step to find anagrams, updating counts as the window slides. Handle edge cases like empty pattern, pattern longer than text, and repeated characters by checking lengths upfront and using efficient data structures.
Pro tip: Emphasize that the frequency comparison can be optimized to O(1) per window by tracking the number of matching characters instead of comparing entire maps, and mention that this approach is optimal for large inputs.
Confirm that both strings are lowercase, discuss handling of empty pattern (return all indices or empty list?), pattern longer than text (return empty list), and repeated characters. This shows thoroughness.
Create frequency arrays (size 26) for the pattern and for the first window of text. Compute the initial number of matches between the two arrays.
Iterate through the text, adding the new character and removing the old character from the window frequency array, updating the match count accordingly. If matches equal 26, record the start index.
Collect all starting indices where the window is a permutation of the pattern and return them.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.