Here is the interview question prompt, presented for reference.
Assume that we are building software to determine how many planes are in the sky.
The data that we have on planes currently in the air comes in the form of a grid, modeled by a matrix array. See below:
[
['.', '.', '.', 'P'],
['.', '.', '.', 'P'],
['P', 'P', '.', 'P'],
['.', '.', '.', 'P']
]
Within that matrix, the dots (.
s) represent available airspace. The P
s that are neighboring each other model one plane each. So in the above example, there are exactly 2
planes.
Given a multi-dimensional array of .
s and P
s exclusively, can you find the number of planes that there are in the sky? Planes may only be placed horizontally or vertically. A diagonal plane like this would not count as one plane. Instead, there will be 3 separate planes.
[
['.', '.', 'P'],
['.', 'P', '.'],
['P', '.', '.']
]
Could you accomplish this in one pass?
sky = [
['.', '.', '.', 'P'],
['.', '.', '.', 'P'],
['P', 'P', '.', 'P'],
['.', '.', '.', 'P']
]
numOfPlanes(sky);
// 2
100000
.
and P
charactersO(n * m)
where n
and m
are number of rows and columns respectivelyO(1)
You can see the full challenge with visuals at this link.
Challenges • Asked over 5 years ago by DanieLao
This is the main discussion thread generated for Count The Planes.
Brilliant solution!
i didnt get should we add diagonal ones as seperate or as one?
Hi Zestymonk,
There was an issue with the problem specification that you are refering to. We have now fixed it and also updated the solution.
Please, fix java tests, they are syntactically incorrect!
> -, -
MainTest.java:125: error: illegal start of expression
public void verticalAndHorizontalPlanesTest() {
^
MainTest.java:124: error: ')' expected
@Test
^
MainTest.java:136: error: illegal start of expression
public void emptyGridTest() {
^
MainTest.java:135: error: ')' expected
@Test
^
4 errors
Apologies -- the Java tests have been fixed!