Panda Guru LogoPanda
Guru

Amazon SDE 6M intern Interview question

Round 1

Questions: You are given a 2D grid representing a farmland where each cell can either be a defender or a farmer. The grid is represented as a 2D array of integers, where 1 denotes a defender and 0 denotes a farmer. Your task is to determine the position of the most vulnerable farmer in the grid. A farmer's vulnerability is defined by the number of defenders present before them in their row.

Objective: Find the position of the most vulnerable farmer, defined as the farmer with the fewest defenders (1s) in front of them in their respective row. If multiple farmers have the same vulnerability, return the position of the first farmer encountered in a row-wise order (top to bottom).

Input:

Output:

Constraints:

Candidate's Approach
  1. Initialize variables to track the minimum number of defenders and the position of the most vulnerable farmer.
  2. Iterate through each row of the grid:
    • For each farmer (0), count the number of defenders (1s) before them in that row.
    • If the count is less than the current minimum, update the minimum and store the farmer's position.
  3. Return the position of the most vulnerable farmer in 1-based indexing.
Interviewer's Feedback

No feedback provided.