Startup | Phone | Line Drawing Algorithm

startup logo
startup
February 15, 20240 reads

Summary

I had a phone interview with a startup where I was given a coding challenge to implement a line drawing algorithm on a grid.

Full Experience

During my phone interview at a startup, I was presented with a coding challenge that focused on drawing a straight line between two given coordinates on a grid. The task required me to implement a function that would mark the path of the line with '1's in a 2D array, aiming for the straightest possible line. I had to consider various edge cases and ensure my solution handled different orientations and lengths effectively.

Interview Questions (1)

Q1
Line Drawing Algorithm on Grid
Data Structures & AlgorithmsMedium

Implement a function drawLine(M: int, N: int, start: tuple[int, int], end: tuple[int, int]) -> [[int]] that draws as straight a line as possible between the given start and end coordinates on an M x N grid. The function should return a grid representing the path of '1's from the start to the end coordinate.

def drawLine(M:int,N:int,start:tuple[int,int],end:tuple[int,int]) -> [[int]]:
  pass

For example:

# Example 1
start=(0,0)
end=(8,2)
M,N=9,5
[
[1,0,0,0,0],
[1,0,0,0,0],
[1,0,0,0,0],
[0,1,0,0,0],
[0,1,0,0,0],
[0,1,0,0,0],
[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0]
]

Example 2

M,N=7,5 start=(6,0) end=(4,4) expected=[ [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,1,1], [0,0,1,0,0], [1,1,0,0,0] ]

Example 3

M,N=4,5 start=1,0 end=1,4 expected=[ [0,0,0,0,0], [1,1,1,1,1], [0,0,0,0,0], [0,0,0,0,0] ]

The line should be as straight as possible from the start to end coordinate. Consider edge cases.

Discussion (0)

Share your thoughts and ask questions

Join the Discussion

Sign in with Google to share your thoughts and ask questions

No comments yet

Be the first to share your thoughts and start the discussion!