← Back to section

Final exam — Matrix

Three unseen problems (not covered in the lessons) to test whether you leave this section able to solve any problem of the topic: one easy, one medium and one hard. Some test cases are hidden — like the real LeetCode. Solve in Python or JavaScript.

Easy

Flood Fill

Given an image (grid of integers), a starting position (sr, sc) and a new color, paint the region connected to the starting cell (same original color, horizontal/vertical neighborhood) with the new color. Return the image.

Example: [[1,1,1],[1,1,0],[1,0,1]], sr=1, sc=1, color=2 → [[2,2,2],[2,2,0],[2,0,1]].

Carregando editor…
Medium

Spiral Matrix II

Given an integer n, generate an n × n matrix filled with the numbers from 1 to n² in spiral order (clockwise, starting from the top-left corner).

Example: n = 3 → [[1,2,3],[8,9,4],[7,6,5]].

Carregando editor…
Hard

Surrounded Regions

In a grid of "X" and "O", capture all regions of "O" that are completely surrounded by "X" (replacing those "O" with "X"). An "O" is not captured if it is connected to an "O" on the edge of the grid. Return the grid.

Example: the central region of "O" that is surrounded becomes "X"; the "O" touching the edge remain.

Carregando editor…