class LightBoard {
private boolean[][] lights;
public LightBoard(int numRows, int numCols) {
lights = new boolean[numRows][numCols];
for (int r = 0; r < numRows; r++) {
for (int c = 0; c < numCols; c++) {
lights[r][c] = Math.random() < 0.4;
}
}
}
}
public class MyProgram {
public static void main(String[] args) {
LightBoard b = new LightBoard(7, 5);
System.out.println("constructed");
}
}
MyProgram.main(null);

Part B: evaluateLight
Determine whether a light should be on or off based on how many lights in its column are currently on.
class LightBoard {
private boolean[][] lights;
public LightBoard(int numRows, int numCols) {
lights = new boolean[numRows][numCols];
for (int r = 0; r < numRows; r++) {
for (int c = 0; c < numCols; c++) {
lights[r][c] = Math.random() < 0.4;
}
}
}
public boolean evaluateLight(int row, int col) {
int onCount = 0;
for (int r = 0; r < lights.length; r++) {
if (lights[r][col]) onCount++;
}
boolean cur = lights[row][col];
if (cur) {
if (onCount % 2 == 0) return false;
} else {
if (onCount % 3 == 0) return true;
}
return cur;
}
}
public class MyProgram {
public static void main(String[] args) {
LightBoard b = new LightBoard(7, 5);
System.out.println(b.evaluateLight(0, 3));
System.out.println(b.evaluateLight(6, 0));
System.out.println(b.evaluateLight(4, 1));
System.out.println(b.evaluateLight(5, 4));
}
}
MyProgram.main(null);

Key Takeaways
In this question, I needed to make sure I correctly created and initialized a 2D boolean array using nested loops, remembering that arrays are traversed row by row. For Part A, my main focus was allocating the array properly and setting each cell to true with a 40% probability using Math.random() < 0.4. For Part B, I had to count how many lights were on in a specific column before applying the rules, then carefully use conditional logic to decide whether to flip the value or keep it the same. Overall, this problem tested whether I understood 2D array traversal, probability generation, and structured decision-making logic.