Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Nested Loop for 2D Array

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Nested Loop for 2D Array. Start with the requirements on the right.

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.

Problem

Samsung Semiconductor represents a wafer inspection area as a rectangular two-dimensional grid. Given the number of rows and cols, return a matrix filled with consecutive integers starting at 0, ordered from left to right and then top to bottom.

Implement the function using a single Python return statement with a nested list comprehension. The inner comprehension must create each row, and the outer comprehension must create the matrix.

Formal Specification

  • Input: two integers, rows and cols.
  • Output: a list containing rows lists, where each inner list contains cols integers.
  • The value at position [r][c] must be r * cols + c.
  • The original inputs must not be modified.

Constraints

  • 1 <= rows <= 100
  • 1 <= cols <= 100
  • The output must contain exactly rows rows.
  • Every row must contain exactly cols values.
  • Rows must be independent list objects.

Function Signature

def build_inspection_grid(rows, cols):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output