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
Draw a Line on a Board
00:00
5 left

Draw a Line on a Board

HardPython

Problem

Paint a board: given a matrix whose cells all start as 'O', draw a line from (x0, y0) to (x1, y1), marking the cells the line passes through. A perfect diagonal such as (0,0) to (3,3) is straightforward, but you must also handle a non-45-degree line such as (0,0) to (4,7). Asked in the onsite coding round 2 stage. The author warns this interviewer was a former Microsoft imaging engineer and reuses this question.

Implement draw_line(board, x0, y0, x1, y1). Coordinates use board[y][x]; mutate and return the board, replacing every rasterized cell with 'X'. Use integer rasterization and include both endpoints. Inputs contain at least one row, and all coordinates are valid.

Constraints

  • 1 <= len(board) <= 1000
  • 1 <= len(board[0]) <= 1000
  • All rows have the same length
  • 0 <= x0, x1 < len(board[0])
  • 0 <= y0, y1 < len(board)
  • The board contains only 'O' before drawing

Function Signature

def draw_line(board, x0, y0, x1, y1):
Interviewer

Your question is Draw a Line on a Board. 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.