Magicsheet logo

Maximum Students Taking Exam

Hard
91.8%
Updated 6/1/2025

Maximum Students Taking Exam

1. What is this problem about?

The Maximum Students Taking Exam problem presents a classroom layout as a matrix of seats, where some are broken ('.') and some are available ('#'). There is a catch: students cannot sit next to each other horizontally or diagonally (left-front or right-front). This prevents them from cheating by looking at each other's work. The goal is to determine the maximum number of students that can be seated in the classroom while following these constraints. It is a classic optimization problem involving spatial restrictions.

2. Why is this asked in interviews?

This is a challenging "Maximum Students Taking Exam interview question" because it requires translating physical constraints into bitmasks and state transitions. Tech giants like SAP and Google use it to evaluate a candidate's proficiency in Dynamic Programming (DP) and bit manipulation. It tests whether you can recognize that the seating choice in one row only affects the choices in the immediate next row, allowing for a row-by-row state-based solution.

3. Algorithmic pattern used

The "Array, Matrix, Bitmask, Bit Manipulation, Dynamic Programming interview pattern" is the standard approach for this problem. Since each row can have a limited number of seats (often <= 8), we can represent the seating arrangement of a row as a bitmask (e.g., 101010). The DP state is defined by the current row index and the bitmask of students in the previous row. We iterate through all possible valid bitmasks for the current row and check if they are compatible with the layout and the mask of the row above.

4. Example explanation

Imagine a 2x3 classroom: Row 1: [ . # . ] (Only middle seat is good) Row 2: [ # # # ] (All seats good)

  • Row 1 valid masks: 000, 010.
  • If we pick 010 for Row 1 (1 student), Row 2 cannot have students at (2,0) or (2,2) because they are diagonally adjacent to the student at (1,1).
  • So for Row 2, the only valid seats given Row 1's 010 are (2,1). Mask: 010.
  • Total students: 1 + 1 = 2.
  • Alternatively, if Row 1 is 000, Row 2 could have 101 (2 students). Total: 2. The algorithm explores these combinations to find the highest total.

5. Common mistakes candidates make

A major pitfall in the "Maximum Students Taking Exam coding problem" is not correctly identifying all cheating constraints. Candidates often forget the diagonal checks or only check the seat directly in front. Another mistake is using an inefficient DP state that doesn't utilize bitmasks, leading to exponential time complexity that exceeds the limits. Failing to pre-filter valid masks for a single row (ignoring broken seats and horizontal neighbors) can also make the main DP loop unnecessarily slow.

6. Interview preparation tip

Start by mastering bitmask basics: how to check if a bit is set, how to set a bit, and how to iterate through all numbers from 0 to 2^N. For this specific problem, practice writing a helper function that determines if a bitmask is "internally valid" (no two '1's are adjacent). Once you have that, the transition between rows becomes much easier to manage. Visualizing the dependencies as a graph of row states can also help you understand the DP transition better.

Similar Questions