Compute Parking Clustering Probability and P-Value
GustoData Scientist · Technical ScreenLast updated
This question tests your ability to calculate exact combinatorial probabilities under a null hypothesis and define a p-value for a spatial clustering test.
The Parking Spot Problem
Imagine a linear parking lot with N total spaces arranged in a single row. There are K cars parked in these spaces, where each car is assigned to exactly one space, and no two cars occupy the same space. Assume that all possible arrangements of the K cars are equally likely (the null hypothesis of spatial randomness).
A "cluster" is defined as a sequence of 2 or more adjacent occupied spaces. For example, if N=5 and K=3, the arrangement [1, 1, 1, 0, 0] contains one cluster of size 3, while [1, 0, 1, 0, 1] contains zero clusters.
Tasks
- Given
Nspaces andKcars, what is the total number of ways to arrange the cars? - Suppose we observe
Mspecific clusters in a given configuration. How would you define the p-value for this observation under the null hypothesis? - For
N=5andK=2, calculate the exact probability of observing at least one cluster of size 2.
Examples
N=3, K=2: Possible arrangements:[1,1,0], [1,0,1], [0,1,1]. Arrangements with at least one cluster:[1,1,0]and[0,1,1]. Probability: 2/3.
Constraints
1 <= K < N- Assume
KandNare small integers for exact calculation.
Hints
Hint 1
The total number of ways to choose K positions out of N is given by the binomial coefficient 'N choose K'.
Hint 2
Under the null hypothesis, the p-value is the probability of observing a test statistic as extreme or more extreme than the one observed, given the distribution of all possible arrangements.
Hint 3
For part 3, list all possible subsets of size 2 from the 5 available spots and count how many have the indices i and i+1 present.
Follow-up questions
- How does the probability change if the parking lot is circular (i.e., space 1 is adjacent to space N)?
- If N and K are very large, how would you approximate this probability distribution using a normal distribution?
- What are the implications for your p-value if the occupancy is not uniform but follows a Poisson process?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.