ML Interview Q Series: Row Seating Probability: Analyzing Friends' Adjacency with Combinatorics
Browse all the Probability Interview Questions here.
Three friends and seven other people are randomly seated in a row, forming a total of ten people. The sample space is all possible ways to seat these ten individuals in a line, so there are 10! equally likely seatings.
(a) What is the probability that the three friends will all sit next to each other?
(b) What is the probability that exactly two of the three friends will sit next to each other?
Short Compact solution
For part (a), there are 8 possible ways to select three consecutive seats out of 10. Within those chosen seats, we can permute the three friends in 3! ways, and then arrange the remaining 7 individuals in 7! ways. Hence the number of favorable seatings is 8 × 3! × 7!. Since the total number of seatings is 10!, the probability that all three friends sit next to each other is (8 × 3! × 7!) / 10!, which equals 1/15.
For part (b), first note there are (3 choose 2) ways to choose which two friends will sit next to each other. Then count separately the scenarios where the pair is at an end of the row versus in the interior. If the pair is at an end seat, there are 2 possible ends, 7 possible ways to choose the exact seats for that pair, and 2! ways to arrange these two friends, along with 7! ways to arrange the remaining 8 people (the third friend plus the 7 other individuals). If the pair is not at an end seat, there are 7 possible positions for that pair in the interior, 6 choices of actual seat placement for them, 2! to permute them, and again 7! for the rest. Summing these possibilities and dividing by 10! yields the probability of exactly two of the three friends being adjacent as 7/15.
Comprehensive Explanation
To understand the reasoning, begin by noting the sample space consists of all ways to seat 10 distinct individuals in a straight row. The size of this sample space is 10!, since the first seat can be occupied by any of the 10 people, the second seat by any of the remaining 9 people, and so on, giving 10 × 9 × ... × 1.
Part (a): Probability all three friends sit together
We want the probability that the three friends occupy a consecutive block of seats. A standard technique in these kinds of problems is to temporarily “group” these three friends as a single object and count how many ways this can happen.
First, figure out how many ways we can select the consecutive block of three seats. Since the row has 10 seats labeled from 1 through 10, the starting seat of that three-seat block can be 1, 2, 3, ..., 8, giving 8 possible blocks. Once we pick a block, we can permute the three friends among themselves in 3! ways. Meanwhile, the other 7 people can be arranged in 7! ways in the remaining seats. As a result, the number of favorable seatings is 8 × 3! × 7!.
Since all 10! seatings are equally likely, the probability is
Here, 8 is the count of blocks where the friends can sit, 3! is how many ways to permute the friends among those three adjacent seats, 7! is how to arrange the other seven people, and 10! is the total number of possible seatings.
Carrying out the arithmetic, it can be shown that 8 × 3! × 7! = (8 × 6) × 7! = 48 × 7!, while 10! = 10 × 9 × 8 × 7!, from which we find the probability to be 1/15.
Part (b): Probability exactly two of the three friends sit together
We want the probability that exactly one pair of friends is adjacent, but the third friend is not next to either of them. A practical way to approach this is:
Choose which two friends will form the adjacent pair. There are (3 choose 2) ways to select that pair.
Compute the number of seatings where these two chosen friends are next to each other, while the third friend is not adjacent to them.
We split this second step into two subcases:
Subcase 1: The pair is seated at one of the two ends of the row.
If the pair sits at an end, there are 2 possible ends (left end or right end), 7 ways to choose the exact pair of seats within that end section (because once you fix the end seat for the pair, you have 7 different places to “slide” the arrangement of the remaining seats), 2! ways to permute the two friends, and 7! ways to arrange the remaining 7 people (including the third friend). Importantly, the third friend must not be adjacent to the pair, so the relevant seats are arranged so that there is no overlap with that pair. The multiplication 2 × 7 × 2! × 7! captures all seatings for this subcase.
Subcase 2: The pair is seated strictly in the interior (i.e., not at an end).
If the pair occupies an interior segment, there are 7 possible positions in the middle for a pair of adjacent seats, 6 ways to slide that pair relative to the possible seats, 2! permutations of the pair, and again 7! to arrange the others. Once more, we ensure the third friend does not sit directly adjacent to the pair, which is why we carefully choose seats so that the pair is placed in distinct interior positions that leave no adjacency possible for the third friend. We get 7 × 6 × 2! × 7! seatings in this scenario.
Summing these two subcases and multiplying by (3 choose 2) for the initial choice of which two friends form the pair, the number of seatings in which exactly two friends are adjacent (and not all three) is:
(3 choose 2) × [ (2 × 7 × 2! × 7!) + (7 × 6 × 2! × 7!) ].
Dividing by 10! gives:
Numerically this simplifies to 7/15.
Potential Follow-up Question 1: Why do we multiply by 3! when counting the block of three friends?
When three friends are grouped as a single block (as in part (a)), we do so only to count how many ways the group of three can be placed as a single “super-person.” But the three friends themselves can be permuted inside that block in 3! ways. Hence, each valid position of that block corresponds to 3! different internal arrangements.
Potential Follow-up Question 2: How would the probability change if the seating were circular instead of in a row?
In a circular arrangement, we often fix one seat (to break symmetries) and then count the ways to place the remaining people. The total number of distinct seatings around a circle with 10 people is (10 – 1)!, which is 9!. The adjacency computations also change because the circle has no “ends,” so each person has two neighbors, and those neighbors wrap around from seat 10 back to seat 1. As a result, the counts of favorable blocks differ. In general, if all three must be next to each other in a circle of 10 seats, we can consider 10 distinct blocks in the circle, then multiply by 3! for internal permutations, and multiply by 7! for the other people if we fix one seat as reference. The approach is quite similar, but the enumerations of possible seat positions must reflect the circular geometry rather than linear geometry.
Potential Follow-up Question 3: Can we verify these results by simulation in Python?
Yes. Below is an illustrative example of a quick random simulation approach that you could use to verify the probabilities:
import math
import random
def all_three_adjacent(seats, friends):
# seats is a list of 10 distinct IDs; friends is a set of the three 'friend' IDs
for i in range(10):
# Check if seats[i], seats[(i+1) % 10], seats[(i+2) % 10] are all in the friend set
if seats[i] in friends and seats[(i+1) % 10] in friends and seats[(i+2) % 10] in friends:
return True
return False
def exactly_two_adjacent(seats, friends):
count_pairs = 0
for i in range(10):
if seats[i] in friends and seats[(i+1) % 10] in friends:
count_pairs += 1
# For exactly two adjacent, we want count_pairs == 1
return count_pairs == 1
N = 10_000_00 # Number of simulations
friends = {0, 1, 2} # Example friend IDs
all_three_count = 0
exactly_two_count = 0
people = list(range(10))
for _ in range(N):
random.shuffle(people)
if all_three_adjacent(people, friends):
all_three_count += 1
elif exactly_two_adjacent(people, friends):
exactly_two_count += 1
prob_all_three = all_three_count / N
prob_exactly_two = exactly_two_count / N
print("Approx. Probability all three adjacent:", prob_all_three)
print("Approx. Probability exactly two adjacent:", prob_exactly_two)
In a large simulation, the numbers should converge close to 1/15 and 7/15 respectively (for a linear arrangement, you would simply modify the checks to remove the wrap-around).
Potential Follow-up Question 4: Are there common mistakes when solving this type of adjacency problem?
One common pitfall is forgetting to exclude cases where the third friend joins the adjacent pair, thus making all three friends adjacent. Another typical mistake is to miscount the possible ways to place the pair at the ends or in the interior, or to overlook the permutations of the two friends and of the other seven people. Ensuring consistent logic for adjacency conditions and carefully separating the different cases are crucial steps to avoid double counting or missing cases.
Below are additional follow-up questions
What if two of the seats are already occupied by specific individuals before the random seating of the remaining eight?
Answer Explanation In this scenario, the sample space changes because instead of having all 10 seats free, we now have 2 seats fixed with known individuals. Consequently, only the remaining 8 seats are available for random assignment of the other 8 people (3 friends + 5 other individuals). This affects both the total number of possible seatings (now 8! instead of 10!) and the counting of favorable cases for adjacency events. Specifically:
You must recalculate the ways the three friends can end up next to each other in the restricted arrangement, taking into account that 2 seats are pre-filled. For instance, those two pre-filled seats might either create new boundaries that block certain sequences or might create adjacency constraints that inadvertently make some seat combinations impossible.
A possible pitfall is to forget that the fixed individuals might occupy one or more seats that break up where blocks of friends can sit, thus reducing the available configurations for adjacency.
For exactly two friends adjacent, you would need to account for whether the third friend can sit next to the pre-seated individuals and whether that can merge or break adjacency sequences.
In essence, you would re-derive the total permutations (8!), then carefully count the new set of favorable permutations for each adjacency scenario (all three friends adjacent, exactly two adjacent, etc.), ensuring you exclude or include relevant seat placements that intersect with the already occupied positions.
How do we handle the case if the three friends are considered indistinguishable rather than distinguishable?
Answer Explanation When the friends are indistinguishable (e.g., three identical items instead of three distinct people), the counts for favorable and total permutations must be modified. Typically, for distinct people, we use permutations of the three friends (factor of 3!) to account for internal re-orderings. But if they are identical:
The factor 3! in favorable counts would vanish. For instance, in part (a) where all three sit together, you no longer multiply by 3! for the internal arrangement of those three identical friends.
You also need to adjust the total permutations: if all 10 individuals were identical in subgroups (say 3 identical friends + 7 distinct other people), you would have 10! / 3! ways to seat them overall.
A common pitfall is forgetting to remove the 3! factor from both the numerator (favorable arrangements) and the denominator (total arrangements) in the correct manner, possibly leading to double-counting or missing permutations.
This changes numerical probabilities because now the relative weighting of certain configurations differs when identical individuals are grouped.
How does the probability change if each of the 10 seats is not equally likely to be chosen by the individuals?
Answer Explanation So far, we have assumed a uniform random distribution over all seatings (i.e., each of the 10! seatings is equally probable). In a more complex scenario, each person might choose a seat based on preferences (say the seats at the ends are more appealing, or certain seats have a higher chance of being chosen). This breaks the uniform assumption, so the sample space is no longer simply 10! equally likely outcomes.
You must then define or estimate the selection probability for each possible arrangement. For instance, if each seat has a distinct probability of being chosen by a particular person, the sample space weights each permutation differently.
To compute the probability that all three friends are adjacent, you would sum (over all permutations) the probability of each permutation where the three friends are adjacent, rather than just dividing a count of permutations by 10!. Each permutation has a probability p_1 × p_2 × ... × p_10 depending on your seat-choice model.
A common pitfall is trying to still count permutations and divide by 10! without factoring in the weighting. Failing to incorporate different seat-choice probabilities can lead to incorrect results.
In practical terms, one often resorts to either enumerating all permutations with their associated probabilities (if feasible) or running Monte Carlo simulations to estimate the adjacency probability.
What if we want to guarantee that no two of the three friends sit together (i.e., all three are separated)? How do we compute that probability?
Answer Explanation This is a complementary type of event to “any adjacency.” The event that all three friends sit separately means no pair of friends is adjacent. To calculate this:
One approach is to count the total permutations (10!), then subtract the permutations where at least two friends are adjacent. You could do this via the Inclusion-Exclusion Principle:
First, sum the number of arrangements where each pair is adjacent.
Subtract the arrangements where at least two different pairs overlap (which would effectively be all three friends together).
Carefully handle any double-counting or triple-counting among those events.
Pitfalls arise if you forget to subtract the arrangements where all three friends are together (since that set is included in each of the adjacency events for pairs) and possibly subtract them multiple times. The Inclusion-Exclusion arithmetic must be done meticulously.
Another direct approach is to seat the 7 non-friends first, creating 8 “gaps” around them (including the ends), and place the 3 friends in those gaps such that no two occupy the same gap. This method also has to consider the permutations of the 7 other individuals, and the ways to distribute the 3 friends among the 8 gaps with at most 1 friend per gap.
How does the reasoning change if the row is very short, e.g., only 5 seats for 3 friends and 2 other people?
Answer Explanation When you dramatically reduce the total number of seats, adjacency patterns can shift:
The sample space would then have 5! arrangements instead of 10!. You’d use the same logic but with smaller numbers, which might actually make the adjacency constraints more restrictive or easier to count directly by enumerating all permutations.
You can manually list all seat arrangements for 5 people. This can be instructive for verifying combinatorial formulas:
For instance, to find the probability that all three friends are together in a row of 5 seats, you might identify exactly which 3-seat blocks can hold them and proceed with a counting approach or direct listing.
A pitfall is to rely on general formula patterns for 10 seats and forget to check whether certain seat blocks become impossible or trivial for smaller seat counts.
Another subtlety is that with fewer seats, it’s more common for “edge” constraints to matter—there may be more overlap between possible seat blocks or fewer ways to separate friends.
Could we approach this by conditioning on the position of a specific friend?
Answer Explanation Sometimes, to simplify adjacency problems, we fix one friend’s seat and reason about possible placements of the other two friends:
For instance, if you fix Friend A in seat number 1, you can count how often Friend B and Friend C end up in seat 2, seat 3, etc.
Then multiply or average across all the seats that Friend A could occupy, if each seat is equally likely for them.
A common pitfall with this approach is forgetting to multiply by the correct factor or failing to realize that conditioning can lead to double-counting across different seat placements. Another oversight might be ignoring the permutations of the non-friend individuals in the rest of the seats.
While it can sometimes reduce the complexity of adjacency counting, one must be consistent in handling all possible seat positions for the first friend. Typically, this method is more prone to mistakes if not handled systematically.
How might we generalize to four or more friends?
Answer Explanation When the problem extends to four or more friends, adjacency calculations become significantly more involved:
For instance, with four friends, you might ask for the probability that at least k of them sit together (k could be 2, 3, or 4). You can attempt direct counting methods similar to the approach used for three friends, but the number of cases multiplies quickly (e.g., pairs, triplets, quadruplets, overlaps among those, etc.).
You can leverage Inclusion-Exclusion once more: you first sum over all configurations with each friend-group adjacency, subtract where multiple friend groups overlap, etc.
A major pitfall is losing track of overlapping adjacency sets (e.g., a block of three friends might also count as multiple overlapping pairs, and a block of four friends might be counted in all pair- and triple-adjacency events).
It’s easy to double-count or triple-count events. Thus, enumerating subcases carefully and perhaps using a combinatorial identity or advanced counting approach (such as generating functions) can sometimes be more reliable.
How do edge effects and seat labeling details matter in real-world seating (e.g., theaters with aisles or partial separations between seats)?
Answer Explanation In reality, some seats may be slightly separated by aisles or physically blocked seats (e.g., accessible seating areas). This can break a row into smaller segments or cause the adjacency notion to become tricky:
If an aisle physically prevents someone in seat 5 from touching someone in seat 6, we might decide that seats 5 and 6 are not adjacent in the usual sense. Then the adjacency counting must be reworked: you effectively have two smaller sub-rows instead of one big row.
Another subtlety is labeling: if seat labeling is ambiguous or if seat 1 is in the middle for some reason, the entire adjacency condition can shift. You must define adjacency precisely for the seats in question (for instance, seat 1 is next to seat 2, seat 2 next to seat 3, etc. — but if seat 4 is across an aisle from seat 5, they might not be considered adjacent).
A pitfall arises when applying a standard adjacency formula that assumes a single continuous row to a scenario that actually includes breaks or partial obstructions. That would lead to overcounting seatings as “adjacent” that are not actually next to one another in the physical layout.
How might results differ if we allow “standing” or empty seats?
Answer Explanation Suppose there are 10 seats, but only 7 people in total (including the 3 friends). Now we have empty seats, which changes both the sample space and adjacency conditions:
The sample space involves choosing which 7 of the 10 seats are occupied and in what order those 7 people occupy them, giving C(10, 7) × 7! total seatings if all seat choices are equally likely.
The event “three friends are adjacent” is trickier because a friend could be next to an empty seat. Adjacency typically requires that the two seats are occupied, so now you need to only look at seats that are both occupied by friends. If a seat in the “middle” of the friends is empty, that breaks adjacency.
Pitfalls include accidentally counting seat-blocks that contain empty spots as if they were valid friend-adjacency sequences. Another mistake is forgetting that the seats themselves still have labels, so you must consider exactly which subset of seats is occupied.
How can one systematically avoid double-counting in these adjacency problems?
Answer Explanation Double-counting is a frequent issue when enumerating adjacency cases, particularly with multiple overlapping events (like multiple pairs). A systematic approach is:
Define events carefully: Let E_ij be the event that friends i and j are adjacent. Then when you want exactly k pairs of friends adjacent, you’re looking at intersections and unions of events E_ij.
Use Inclusion-Exclusion: For instance, to count the total number of seatings where E_12 or E_13 or E_23 occurs, you sum the seatings in each event, subtract seatings in all pairwise intersections (E_12 ∩ E_13, etc.), and add the seatings in triple intersections if needed.
Label subcases concretely: e.g., “All three friends are adjacent” is E_12 ∩ E_13, which you can count with the block-of-three approach. Then “exactly two friends are adjacent” typically is (E_12 ∪ E_13 ∪ E_23) minus the cases in which all three are adjacent.
Check boundary conditions: e.g., seats at the ends or special seat numbering.
Validate with smaller seat examples or with partial enumeration if you’re unsure.
A major pitfall: mixing up the events “E_12 ∩ E_13” (i.e., friend 1 is next to both friend 2 and friend 3, which implies all three are together) with “E_12 and E_23” (which might require friend 2 sits in between 1 and 3). Clear definitions keep the logic consistent.