Place Widgets on a Dashboard Grid Satisfying Row and Column Orders

Last updated: November 29, 2026

Quick Overview

Place k widgets numbered 1..k on a k by k grid, one per row and one per column, given row constraints [a, b] (a must be in a strictly higher row than b) and column constraints [l, r] (l must be strictly left of r). Return any valid grid with zeros in empty cells, or an empty list if impossible.

Meta
Coding & Algorithms
Frontend Engineer
Meta
November 29, 2026
Frontend Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Place k widgets numbered 1..k on a k by k grid, one per row and one per column, given row constraints [a, b] (a must be in a strictly higher row than b) and column constraints [l, r] (l must be strictly left of r). Return any valid grid with zeros in empty cells, or an empty list if impossible.

A Meta onsite question framed around laying out widgets on an Ads Manager or internal analytics dashboard under ordering rules from designers. The interviewer checks that you notice rows and columns are independent, so two separate topological sorts solve it, and that you place each widget at (row position, column position).

What the Interviewer Expects
  • Recognize row and column constraints are independent
  • Run one topological sort per dimension
  • Map each widget to its index in each order
  • Return empty if either constraint set has a cycle
  • Explain why giving every widget its own row and column is always allowed
Key Topics to Cover
topological sort
independent dimensions
Kahn's algorithm
grid construction
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
  • What if multiple widgets may share a row as long as constraints hold?
  • How would you make the layout as compact as possible?
  • How would you handle widgets with different widths?
Sharpen Your Skills on Codemia

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

Practice DSA Problems
Sample Answer
Requirements
  • Input: k, rows (list of [above, below]), cols (list of [left, right]).
  • Output: a k x k grid where each number 1..k appears exactly once and constraints hold, or [].
  • Since there a...
High-Level Design

Row constraints only talk about vertical order and column constraints only about horizontal order, so the two can be solved separately:

  1. Topologically sort 1..k under rows to get row_order. W...

Submit Your Answer
Markdown supported