Remove duplicate house colors and sort each block of a neighborhood

Quick Overview

Given a neighborhood as a list of blocks of house colors, remove the duplicate colors within each block and return every block sorted in ascending order. It tests per-row processing of a 2D array, set-based deduplication and sorting, plus the edge cases that hide a missing sort.

Remove duplicate house colors and sort each block of a neighborhood

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a neighborhood represented as a 2D array `blocks`, where each `blocks[i]` is an array of integers representing the house colors in the i-th block. For each block, remove duplicate house colors and sort the remaining colors in ascending order. Return the resulting neighborhood, where each block contains only uniquely colored houses in sorted order. ### Function Signature ```python def shuffle_neighborhood(blocks: list[list[int]]) -> list[list[int]]: ``` ### Rules - Duplicates are removed within a block only. The same color can appear in several blocks of the output. - The output has one list per input block, in the same order as the input. - Each output list contains every distinct color of its block exactly once, in strictly increasing order. ### Constraints - `1 <= blocks.length <= 100` - `1 <= blocks[i].length <= 100` - `1 <= blocks[i][j] <= 1000` ### Examples ```text Input: blocks = [[3,1,2,3,1],[5,5,4,3],[7,8,7,9,8]] Output: [[1,2,3],[3,4,5],[7,8,9]] ``` Block 0 holds the colors 1, 2 and 3 with repeats, so it becomes [1,2,3]. Block 1 reduces to {3,4,5} and block 2 to {7,8,9}, each written in ascending order. ```text Input: blocks = [[1],[2,2,2],[3,1,2]] Output: [[1],[2],[1,2,3]] ``` The single house in block 0 stays as it is, the three houses of color 2 in block 1 collapse to one, and block 2 has no repeats but still has to be sorted.

Overview: Given a neighborhood as a list of blocks of house colors, remove the duplicate colors within each block and return every block sorted in ascending order. It tests per-row processing of a 2D array, set-based deduplication and sorting, plus the edge cases that hide a missing sort.

Google logo
Google
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a neighborhood represented as a 2D array blocks, where each blocks[i] is an array of integers representing the house colors in the i-th block.

For each block, remove duplicate house colors and sort the remaining colors in ascending order. Return the resulting neighborhood, where each block contains only uniquely colored houses in sorted order.

Function Signature

def shuffle_neighborhood(blocks: list[list[int]]) -> list[list[int]]:

Rules

  • Duplicates are removed within a block only. The same color can appear in several blocks of the output.
  • The output has one list per input block, in the same order as the input.
  • Each output list contains every distinct color of its block exactly once, in strictly increasing order.

Constraints

  • 1 <= blocks.length <= 100
  • 1 <= blocks[i].length <= 100
  • 1 <= blocks[i][j] <= 1000

Examples

Input: blocks = [[3,1,2,3,1],[5,5,4,3],[7,8,7,9,8]]
Output: [[1,2,3],[3,4,5],[7,8,9]]

Block 0 holds the colors 1, 2 and 3 with repeats, so it becomes [1,2,3]. Block 1 reduces to {3,4,5} and block 2 to {7,8,9}, each written in ascending order.

Input: blocks = [[1],[2,2,2],[3,1,2]]
Output: [[1],[2],[1,2,3]]

The single house in block 0 stays as it is, the three houses of color 2 in block 1 collapse to one, and block 2 has no repeats but still has to be sorted.