Measure Bird Species Segregation
Company: Google
Role: Data Scientist
Category: Statistics & Math
Difficulty: medium
Interview Round: Technical Screen
You are a data scientist analyzing bird observations from a forest. The ecology team wants to know whether different bird species are **spatially segregated** — that is, whether birds tend to be near members of their own species and farther from other species than you would expect if the species were randomly mixed across the same locations.
You are given an observation dataset with the following fields:
| Field | Description |
|-------|-------------|
| `bird_id` | Unique identifier for each observed bird |
| `species` | Species label |
| `x_m`, `y_m` | Location coordinates in meters within the forest |
| `timestamp` | Observation time |
| `plot_id` | Survey plot or transect identifier |
| `observer_id` | Person who collected the observation |
| `effort_minutes` | Survey effort for that plot or transect |
| `habitat_type`, `canopy_density`, `elevation`, `distance_to_water` | Optional habitat covariates |
Design an approach to measure whether species are spatially segregated. Your answer should address each of the parts below.
### Constraints & Assumptions
- This is an **observational** dataset, not a controlled experiment — locations and species labels are both fixed by what was actually observed, and survey effort is uneven across plots.
- Species abundances are **unequal**: a few species are common and many are rare (some with only a handful of observations).
- "Segregation" should be judged **relative to a null of random mixing**, not relative to a uniform spread of birds across the forest.
- Treat the goal as **describing and testing a pattern**, not proving a behavioral cause; causal claims (e.g. active avoidance vs. habitat preference) require justification from the design.
### Clarifying Questions to Ask
- At what **spatial scale** does the team care about segregation — local (territory / a few meters), plot-level (tens of meters), or landscape gradients (hundreds of meters)? Segregation can exist at one scale and not another.
- Do they want **within-species clustering**, **between-species avoidance**, or both? These are related but distinct.
- Should the analysis hold **habitat constant**, or is habitat-driven separation itself considered a form of segregation worth reporting?
- How was sampling done — are `x_m`, `y_m` exact point locations, or do they inherit the centroid of a `plot_id`? This determines which spatial methods are valid.
- Is there a **detectability** concern (some species harder to spot, or some observers more skilled)? Should `observer_id`, `timestamp`/season, and `effort_minutes` be treated as confounders?
- Is there a minimum sample size below which species-level conclusions should be suppressed?
---
### Part 1 — Define segregation and choose at least one metric
State precisely what "segregated" means operationally, then propose at least one quantitative metric that captures it. Be explicit about what the metric measures and at what spatial scale it operates.
```hint Where to start
Pin down the definition before the metric. "Same species closer than expected, other species farther than expected, under random mixing" gives you something testable. Note that **within-species clustering** and **between-species avoidance** are distinct and may need different metrics.
```
```hint A simple metric to anchor on
Consider a **nearest-neighbor** statistic: for each bird, is its nearest neighbor the same species? Compare the observed same-species rate to what random label assignment would give. With species proportions $p_1,\dots,p_K$, what same-species nearest-neighbor rate would you expect by chance?
```
```hint Going beyond a single scale
A single number hides scale. Think about **spatial point-pattern** tools (e.g. cross-type $K$ or $L$ functions) that measure co-occurrence of two species as a function of radius $r$, or **grid/plot-based dissimilarity** indices that compare species composition across cells. Each answers "segregated at what distance?"
```
#### What This Part Should Cover
```premium-lock What This Part Should Cover
```
### Part 2 — State the null hypothesis
Write down the null hypothesis your test assumes, and justify why it is the right baseline for an observational dataset with uneven sampling.
```hint What to hold fixed
A naive null is "birds are placed completely at random in the forest" — but that ignores where you actually surveyed. A stronger null **fixes the observed locations** and asks only whether the **species labels** are arranged more separately than chance. What does the null then say about the relationship between species identity and location?
```
#### What This Part Should Cover
```premium-lock What This Part Should Cover
```
### Part 3 — Test statistical significance
Describe how you would obtain a p-value (or equivalent evidence) for your metric under that null.
```hint Technique to consider
The null ("labels are exchangeable across the observed locations") lends itself to a **permutation / randomization test** rather than a closed-form distribution. Sketch the resample loop and how you'd turn the resulting reference distribution into a p-value. Be careful about how you estimate a tail probability from a *finite* number of permutations — what does the naive count give you when the observed value happens to be the most extreme, and is reporting an exact 0 honest?
```
```hint Don't forget multiplicity
With $K$ species there are $\frac{K(K-1)}{2}$ pairwise comparisons. What correction (FDR, Bonferroni, or hierarchical pooling) keeps your false-positive rate honest?
```
#### What This Part Should Cover
```premium-lock What This Part Should Cover
```
### Part 4 — Handle the practical issues
Explain how your approach addresses each of the following real-world complications. These are where the interviewer is looking for depth.
- **Uneven species abundance** (common vs. rare species)
- **Habitat differences** (species preferring different `habitat_type` / `canopy_density` / etc.)
- **Spatial scale** (results differ at 10 m vs. 100 m)
- **Sampling bias** (uneven `effort_minutes`, `observer_id` effects, edge of the survey region)
- **Rare species** (few observations → unstable estimates)
```hint Confounding from habitat & effort
If species A likes wetlands and B likes dry forest, they look segregated without ever avoiding each other. Think about two broad ways to neutralize this: can you **hold habitat roughly constant** while you randomize (i.e. constrain *where* the label shuffle is allowed to move things), or can you instead **model the environment out** — predict each species' density from habitat and survey effort, and then ask what co-occurrence is left over? Which fields would each approach lean on?
```
```hint Edge effects and rare species
Birds near the boundary have unobserved space beyond it, biasing neighbor-distance and $K$-function estimates — think **edge correction** or buffering. For rare species, think **minimum sample thresholds**, **uncertainty intervals**, and **partial pooling** rather than over-interpreting a 5-observation species.
```
#### What This Part Should Cover
```premium-lock What This Part Should Cover
```
### What a Strong Answer Covers
```premium-lock What a Strong Answer Covers
```
### Follow-up Questions
- How does your conclusion change if `x_m`, `y_m` are not true point locations but the centroid of each `plot_id`? Which methods break, and which still hold?
- Suppose two species are still negatively associated *after* you control for habitat and effort — what additional evidence would you need before calling it active avoidance rather than an unmeasured environmental gradient?
- The dataset spans multiple seasons. How would you incorporate `timestamp` so that seasonal turnover in which species are present doesn't masquerade as spatial segregation?
- If the team wants a single executive-summary number for "how segregated is this forest overall," how would you aggregate scale- and pair-dependent results responsibly, and what would you caveat?
Quick Answer: This question evaluates spatial statistical reasoning, ecological data analysis, and the ability to formulate hypotheses, choose appropriate segregation metrics, and account for confounders and sampling bias.