Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Rectangle Fill and Bring-To-Top
00:00
5 left

Rectangle Fill and Bring-To-Top

HardPython

Problem

Given an MxN grid initialized with 0s, implement FillShape(lowerLeft, topRight, char) to fill a rectangle (shapes may overlap) and BringTop(r,c) to bring the whole rectangle that the char at (r,c) belongs to back on top.

Asked in the video screen stage.

Implement the following operations on a grid of characters:

  1. FillShape(lowerLeft, topRight, char) fills the axis-aligned rectangle, inclusive of both corners.
  2. BringTop(r, c) finds the most recent rectangle covering cell (r, c) and moves that entire rectangle to the top of the stacking order.

Return the final grid after all operations. Assume later fills overwrite earlier ones within overlapping regions, and BringTop only changes layering order, not rectangle boundaries.

Constraints

  • 1 <= m, n <= 200
  • 0 <= lowerLeft[0] <= topRight[0] < m
  • 0 <= lowerLeft[1] <= topRight[1] < n
  • Each operation is either FillShape(lowerLeft, topRight, char) or BringTop(r, c)
  • At least one FillShape operation is present
  • Characters are single printable non-zero values

Function Signature

def solve(m, n, operations):
Interviewer

Your question is Rectangle Fill and Bring-To-Top. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.