PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Google

Construct connected crop layout and safe paths

Last updated: Mar 29, 2026

Quick Overview

This question evaluates constructive grid design and graph-based pathfinding skills, specifically the ability to produce connected labeled regions that meet size constraints and to reason about shortest paths and path-safety metrics based on Manhattan distance.

  • hard
  • Google
  • Coding & Algorithms
  • Machine Learning Engineer

Construct connected crop layout and safe paths

Company: Google

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

## Problem A — Construct a garden with connected crop regions You are given an `N × M` rectangular grid (a garden) that must be fully planted using `k` crop types labeled `a, b, ..., k`. You are also given required counts for each crop: `counts = [c1, c2, ..., ck]` such that: - `c1 + c2 + ... + ck = N * M` ### Task Construct and output **any** `N × M` grid assignment of crop labels such that: 1. Every cell is assigned exactly one crop. 2. Crop `i` appears exactly `ci` times. 3. For each crop type, all its cells form **one connected component** under **4-directional adjacency** (up/down/left/right). If it is impossible, output that it cannot be done. ### Follow-up (non-rectangular garden) Now the garden is the union of two squares: - One square of size `N × N` - One square of size `M × M` They are **stacked vertically** with their **left edges aligned**, and they **do not overlap**. (So the overall shape may be non-rectangular if `N ≠ M`.) You are given `k` crops with counts summing to `N^2 + M^2`. Construct and output a planting for this shape such that each crop’s cells are 4-connected within the shape. --- ## Problem B — Grid shortest path and maximize distance from a cat You are given an `N × N` grid where: - `0` = land (walkable) - `1` = water (blocked) You are also given two land cells: - `S` = start - `T` = target Movement is allowed only on land cells and only in 4 directions. ### Task 1 Find the **shortest path** from `S` to `T` (or the shortest path length). If no path exists, report failure. ### Follow-up (maximize minimum Manhattan distance to a cat) In addition, one cell contains a cat at position `C` (not necessarily on the path). For any path `P` from `S` to `T` (moving only on land), define the path’s “safety” as: \[ \text{safety}(P) = \min_{v \in P} \; \text{ManhattanDistance}(v, C) \] where Manhattan distance is `|r1 - r2| + |c1 - c2|`. Find a path from `S` to `T` that **maximizes** this safety value (i.e., maximizes the minimum Manhattan distance to the cat among all cells on the path). If no path exists, report failure. (You may return either the maximum achievable safety value, or the corresponding path.)

Quick Answer: This question evaluates constructive grid design and graph-based pathfinding skills, specifically the ability to produce connected labeled regions that meet size constraints and to reason about shortest paths and path-safety metrics based on Manhattan distance.

Related Interview Questions

  • Compute Turnstile Crossing Times - Google (hard)
  • Simulate In-Place Cellular State Updates - Google (hard)
  • Determine Whether a Word Exists in a Graph - Google (medium)
  • Solve Shortest Paths and Rental Allocation - Google (medium)
  • Solve Two Array Optimization Problems - Google (medium)
Google logo
Google
Feb 3, 2026, 12:00 AM
Machine Learning Engineer
Technical Screen
Coding & Algorithms
1
0
Loading...

Problem A — Construct a garden with connected crop regions

You are given an N × M rectangular grid (a garden) that must be fully planted using k crop types labeled a, b, ..., k.

You are also given required counts for each crop: counts = [c1, c2, ..., ck] such that:

  • c1 + c2 + ... + ck = N * M

Task

Construct and output any N × M grid assignment of crop labels such that:

  1. Every cell is assigned exactly one crop.
  2. Crop i appears exactly ci times.
  3. For each crop type, all its cells form one connected component under 4-directional adjacency (up/down/left/right).

If it is impossible, output that it cannot be done.

Follow-up (non-rectangular garden)

Now the garden is the union of two squares:

  • One square of size N × N
  • One square of size M × M

They are stacked vertically with their left edges aligned, and they do not overlap. (So the overall shape may be non-rectangular if N ≠ M.)

You are given k crops with counts summing to N^2 + M^2.

Construct and output a planting for this shape such that each crop’s cells are 4-connected within the shape.

Problem B — Grid shortest path and maximize distance from a cat

You are given an N × N grid where:

  • 0 = land (walkable)
  • 1 = water (blocked)

You are also given two land cells:

  • S = start
  • T = target

Movement is allowed only on land cells and only in 4 directions.

Task 1

Find the shortest path from S to T (or the shortest path length). If no path exists, report failure.

Follow-up (maximize minimum Manhattan distance to a cat)

In addition, one cell contains a cat at position C (not necessarily on the path).

For any path P from S to T (moving only on land), define the path’s “safety” as:

safety(P)=min⁡v∈P  ManhattanDistance(v,C)\text{safety}(P) = \min_{v \in P} \; \text{ManhattanDistance}(v, C)safety(P)=minv∈P​ManhattanDistance(v,C)

where Manhattan distance is |r1 - r2| + |c1 - c2|.

Find a path from S to T that maximizes this safety value (i.e., maximizes the minimum Manhattan distance to the cat among all cells on the path). If no path exists, report failure.

(You may return either the maximum achievable safety value, or the corresponding path.)

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Google•More Machine Learning Engineer•Google Machine Learning Engineer•Google Coding & Algorithms•Machine Learning Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.