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
Greedy Mobile Grid Layout
00:00
5 left

Greedy Mobile Grid Layout

MediumPython

Problem

At PixelFlow, a mobile screen must display identical rectangular cards in a grid. Write a function that calculates the optimal layout for a given screen width, card minimum width, gap size, and number of cards.

The goal is to place as many columns as possible while ensuring each card is at least min_card_width pixels wide. Among all valid layouts, choose the one with the maximum number of columns. Then compute the resulting card width and the number of rows needed.

Formal Specification

Implement a function that receives:

  • screen_width: integer total width of the screen in pixels
  • min_card_width: integer minimum allowed width for each card
  • gap: integer horizontal gap between adjacent cards
  • item_count: integer number of cards to place

Return a list [columns, card_width, rows] where:

  • columns is the chosen number of columns
  • card_width is the integer width of each card after distributing available space evenly
  • rows is the number of rows required to place all cards

If item_count is 0, return [0, 0, 0].

Constraints

  • 0 <= item_count <= 10^5
  • 1 <= min_card_width <= screen_width <= 10^9
  • 0 <= gap <= 10^6
  • At least one column is always possible when item_count > 0

Function Signature

def optimal_layout(screen_width, min_card_width, gap, item_count):
Interviewer

Your question is Greedy Mobile Grid Layout. 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.