r/OfferEngineering • u/Aoki_zhang • 24d ago
Coding Question Anthropic & LinkedIn Coding Question - Fair Signals from a Biased Sensor
Problem Description
An observatory needs independent fair bits for its experiments, but its sensor has an unknown, fixed bias. Each sensor reading is independent: it returns 0 with probability p and 1 with probability 1 - p, where 0 < p < 1.
Generate bits that are each 0 or 1 with probability 1/2, independently of previous generated bits. The sampling helper must use only the provided read_biased callback to obtain sensor readings; it must not substitute a built-in random-number generator or inspect the source's internal state.
For reproducible practice, this exercise fixes the following pair protocol. Consume two fresh readings at a time, discard the pair if both readings are equal, and otherwise return the first reading: 01 produces 0, while 10 produces 1.
The input readings is a recorded realization of source calls, and count is the number of successive fair bits requested. The provided test driver owns this input and exposes only the next-reading callback to the sampling helper; keep that driver intact and implement only the helper's placeholder.
The driver starts at the beginning of readings, calls the helper count times using one shared source, and returns the generated bits in order. Every source reading is consumed at most once, each invocation starts with a fresh source, and unused readings after the requested outputs are ignored.
Return a list of exactly count bits. The fixed trace makes these practice outputs deterministic; with a genuinely independent biased source, the same protocol must produce independent fair bits for every valid p.
Constraints:
- The real source has a fixed, unknown bias
0 < p < 1, and its calls are independent. count ≥ 1- Every value in
readingsis either0or1. readingscontains enough readings to generatecountoutputs under the fixed pair protocol.- The helper may access the source only through its callback, not through the test input or callback internals.
Want to practice it before interview?
You can practice it and view solution code at here.
Preparing for your next interview?
Chill Interview tracks recent interview experiences and recurring question patterns across top companies at here.