Count Seatings Where Every Seat and Guest Number Divide Evenly

Last updated: November 29, 2026

Quick Overview

Guests numbered 1 to n are seated in seats numbered 1 to n, one per seat. A seating is valid when, for every seat i, either the guest number divides i or i divides the guest number. Count the valid seatings.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Guests numbered 1 to n are seated in seats numbered 1 to n, one per seat. A seating is valid when, for every seat i, either the guest number divides i or i divides the guest number. Count the valid seatings.

A Google technical screen that rewards filling positions in a smart order. The interviewer checks a used-mask backtracker and asks whether filling from the most constrained seat first (high indices) speeds it up, then may push for bitmask memoization.

What the Interviewer Expects
  • Backtracks by seat, trying each unused guest that satisfies divisibility
  • Uses a bitmask for used guests
  • Fills seats from n down to 1 because high seats have the fewest options
  • Adds memoization on the used mask since the seat index is implied by the mask's popcount
  • Explains complexity before and after memoization
Key Topics to Cover
Backtracking
Bitmask
Memoization
Constraint ordering
Divisibility
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Add memoization and state the new complexity.
  • How would you list the seatings instead of counting them?
  • How would the answer change for a different compatibility rule, such as sharing a common factor?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: n, up to about 15.
  • Output: the number of permutations p with p[i] % i == 0 or i % p[i] == 0 for all seats i (1-indexed).
  • Example: n = 2 gives 2, n = 3 gives 3....
High-Level Design

Assign seats one at a time. For seat i, loop over guests g not yet used with g % i == 0 or i % g == 0, mark g used, recurse to the next seat, unmark.

Order matters for speed. Seat 1 accepts any gue...


Submit Your Answer
Markdown supported