Problem Set (Coding)
You are given multiple coding questions from an interview. Implement each function as described.
1) Course Overlap
You are given an array of pairs (studentId, courseName) describing which student took which course.
Task: For every pair of distinct students (idA, idB) (where idA < idB), compute the list of course names that both students have taken.
Input:
-
String[][] enrollments
, where each element is
{studentId, courseName}
.
Output:
-
A list of entries, one per student pair, each containing:
-
the pair key as a string like
"idA,idB"
-
the list of common courses (order does not matter, but should be deterministic if you choose to sort)
-
Include pairs even if they share no courses (then the list is empty).
Example
Input:
[ ["17","Math"], ["17","English"], ["58","Math"], ["61","Physics"] ]
Output (one acceptable format):
(
"17,58" -> ["Math"],
"17,61" -> [],
"58,61" -> []
)
2) Validate Sudoku-like Grid
Given an n x n integer grid, validate whether it is a valid Latin-square style grid:
A grid is valid if:
-
n >= 1
-
Every value is an integer in
[1, n]
-
Each row contains each number
1..n
exactly once
-
Each column contains each number
1..n
exactly once
Return true if valid, otherwise false.
Examples
grid = [
[1,2,3],
[2,3,1],
[3,1,2]
]
=> true
grid = [
[0,2,3],
[2,3,1],
[3,1,2]
]
=> false (0 is out of range)
grid = [
[1,3],
[2,1]
]
=> false (3 is out of range for n=2)
3) Validate Nonogram
A Nonogram board is a R x C grid of characters where each cell is:
You are also given:
-
rowClues
: an array of length
R
, where
rowClues[i]
is a list of run-lengths of consecutive
'B'
groups in row
i
(left to right).
-
colClues
: an array of length
C
, where
colClues[j]
is a list of run-lengths of consecutive
'B'
groups in column
j
(top to bottom).
Task: Return true if the board matches all row and column clues; otherwise return false.
Notes:
-
An empty clue list (e.g.,
[]
) means there are
no
black cells in that row/column.
Example
matrix = [
[W,W,W,W],
[B,W,W,W],
[B,W,B,B],
[W,W,B,W],
[B,B,W,W]
]
rowClues = [ [], [1], [1,2], [1], [2] ]
colClues = [ [2,1], [1], [2], [1] ]
=> true
If the clues do not match the derived runs, return false.