Questions on Recursion
Questions on Recursion
CODING QUESTIONS
Flood Filling
An image is represented by an m x n integer grid image where image[i]
[j] represents the pixel value of the image.
You are also given three integers sr, sc, and color. You should perform
a ood ll on the image starting from the pixel image[sr][sc].
To perform a ood ll, consider the starting pixel, plus any pixels
connected 4-directionally to the starting pixel of the same color as the
starting pixel, plus any pixels connected 4-directionally to those pixels
(also with the same color), and so on. Replace the color of all of the
aforementioned pixels with color.
Input: Output:
4 [[2, 2, 3], [7]]
2367
7
ff
Special Keyboard
Imagine you have a special keyboard with the following keys:
Key 1: Prints 'A' on screen
Key 2: (Ctrl-A): Select screen
Key 3: (Ctrl-C): Copy selection to bu er
Key 4: (Ctrl-V): Print bu er on screen appending it after what has already
been printed.
Input: Output:
3 3
ff
ff
Count all possible paths from top
left to bottom right of a mXn matrix
The problem is to count all the possible paths from the top left to the
bottom right of a M X N matrix with the constraints that from each cell
you can either move only to the right or down
Input: Output:
23 3
Josephus Problem
There are N people standing in a circle waiting to be executed. The
counting out begins at some point in the circle and proceeds around the
circle in a xed direction. In each step, a certain number of people are
skipped and the next person is executed. The elimination proceeds around
the circle (which is becoming smaller and smaller as the executed people
are removed), until only the last person remains, who is given freedom.
Input: Output:
52 2
fi