Topological Sort on grid

Last updated: December 5, 2025

Quick Overview

Given a 2D grid consisting of cells that can either be empty or contain a directed edge to another cell, implement a function to perform a topological sort on the grid. The function should return a list of cells in a valid topological order, or indicate if a cycle exists in the directed graph represented by the grid. The input will be a 2D array, and the output should be a list of coordinates representing the sorted order or an indication of a cycle.

Palo Alto Networks
Coding & Algorithms
Software Engineer
Palo Alto Networks
December 5, 2025
Software Engineer
Onsite
Coding & Algorithms
Hard

120

12

2,898 solved


Given a 2D grid consisting of cells that can either be empty or contain a directed edge to another cell, implement a function to perform a topological sort on the grid. The function should return a list of cells in a valid topological order, or indicate if a cycle exists in the directed graph represented by the grid. The input will be a 2D array, and the output should be a list of coordinates representing the sorted order or an indication of a cycle.

Palo Alto Networks uses this problem in the Onsite to evaluate your algorithmic thinking. They expect you to discuss multiple approaches, analyze trade-offs between them, and implement the optimal solution with clean, readable code.

What the Interviewer Expects
  • Quickly identify the optimal approach and its theoretical basis
  • Handle complex algorithm design with multiple interacting components
  • Write concise, elegant code under time pressure
  • Prove correctness of your approach and discuss alternative solutions
  • Optimize beyond the obvious: discuss constant factor improvements
  • Address follow-up variations and explain how the solution generalizes
Key Topics to Cover
Hash maps and frequency counting
Edge cases and input validation
Graph algorithms and traversal
Sorting and searching
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you test this solution thoroughly?
  • How would your solution change if the input was sorted?
  • What is the worst-case input for your solution?
  • Can you solve this iteratively instead of recursively (or vice versa)?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

This problem can be approached as a graph problem where each cell in the grid represents a node, and the directed edges represent dependencies (i.e., which cell must be processed before another). A to...

Approach
  1. Graph Representation: Convert the 2D grid into a graph representation using an adjacency list. Each cell will point to its directed neighbors based on the directed edges defined in the grid. 2....

Submit Your Answer
Markdown supported

Related Questions