Skip to content

Instantly share code, notes, and snippets.

@prianichnikov
Created November 7, 2023 14:41
Show Gist options
  • Save prianichnikov/da223d68656f701b4eb932dbdc8faf4a to your computer and use it in GitHub Desktop.
Save prianichnikov/da223d68656f701b4eb932dbdc8faf4a to your computer and use it in GitHub Desktop.
Technical interview task
/*
We have a two-dimensional board game involving snakes. The board has two types of squares on it: +'s represent
impassable squares where snakes cannot go, and 0's represent squares through which snakes can move.
Snakes can only enter on the edges of the board, and each snake can move in only one direction.
We'd like to find the places where a snake can pass through the entire board, moving in a straight line.
Here is an example board:
col--> 0 1 2 3 4 5 6
+----------------------
row 0 | + + + 0 + 0 0
| 1 | 0 0 + 0 0 0 0
| 2 | 0 0 0 0 + 0 0
v 3 | + + + 0 0 + 0
4 | 0 0 0 0 0 0 0
Write a function that takes a rectangular board with only +'s and 0's, and returns two collections:
* one containing all of the row numbers whose row is completely passable by snakes, and
* the other containing all of the column numbers where the column is completely passable by snakes.
Sample Inputs:
board1 = [['+', '+', '+', '0', '+', '0', '0'],
['0', '0', '+', '0', '0', '0', '0'],
['0', '0', '0', '0', '+', '0', '0'],
['+', '+', '+', '0', '0', '+', '0'],
['0', '0', '0', '0', '0', '0', '0']]
board2 = [['+', '+', '+', '0', '+', '0', '0'],
['0', '0', '0', '0', '0', '+', '0'],
['0', '0', '+', '0', '0', '0', '0'],
['0', '0', '0', '0', '+', '0', '0'],
['+', '+', '+', '0', '0', '0', '+']]
board3 = [['+', '+', '+', '0', '+', '0', '0'],
['0', '0', '0', '0', '0', '0', '0'],
['0', '0', '+', '+', '0', '+', '0'],
['0', '0', '0', '0', '+', '0', '0'],
['+', '+', '+', '0', '0', '0', '+']]
board4 = [['+']]
board5 = [['0']]
All test cases:
findPassableLanes(board1) => Rows: [4], Columns: [3, 6]
findPassableLanes(board2) => Rows: [], Columns: [3]
findPassableLanes(board3) => Rows: [1], Columns: []
findPassableLanes(board4) => Rows: [], Columns: []
findPassableLanes(board5) => Rows: [0], Columns: [0]
Complexity Analysis:
r: number of rows in the board
c: number of columns in the board
*/
public class Solution {
public static void main(String[] args) {
int[][] board1 = {{'+', '+', '+', '0', '+', '0', '0'},
{'0', '0', '+', '0', '0', '0', '0'},
{'0', '0', '0', '0', '+', '0', '0'},
{'+', '+', '+', '0', '0', '+', '0'},
{'0', '0', '0', '0', '0', '0', '0'}};
int[][] board2 = {{'+', '+', '+', '0', '+', '0', '0'},
{'0', '0', '0', '0', '0', '+', '0'},
{'0', '0', '+', '0', '0', '0', '0'},
{'0', '0', '0', '0', '+', '0', '0'},
{'+', '+', '+', '0', '0', '0', '+'}};
int[][] board3 = {{'+', '+', '+', '0', '+', '0', '0'},
{'0', '0', '0', '0', '0', '0', '0'},
{'0', '0', '+', '+', '0', '+', '0'},
{'0', '0', '0', '0', '+', '0', '0'},
{'+', '+', '+', '0', '0', '0', '+'}};
int[][] board4 = {{'+'}};
int[][] board5 = {{'0'}};
System.out.println(findPassableLanes(board1));
System.out.println(findPassableLanes(board2));
System.out.println(findPassableLanes(board3));
System.out.println(findPassableLanes(board4));
System.out.println(findPassableLanes(board5));
}
static String findPassableLanes(int[][] board) {
String rows = "";
String columns = "";
for (int row = 0; row < board.length; row++) {
boolean passableRow = true;
for (int column = 0; column < board[row].length; column++) {
if (board[row][column] == '+') {
passableRow = false;
break;
}
}
if (passableRow) {
if (!rows.isEmpty()) {
rows += ",";
}
rows += row;
}
}
for (int column = 0; column < board[0].length; column++) {
boolean passableColumn = true;
for (int row = 0; row < board.length; row++) {
if (board[row][column] == '+') {
passableColumn = false;
break;
}
}
if (passableColumn) {
if (!columns.isEmpty()) {
columns += ",";
}
columns += column;
}
}
return String.format("Rows: [%s], Columns: [%s]", rows, columns);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment