XOR Pseudorandom Sequence
Updated
Problem statement
Generate a sequence of numbers by updating a 32-bit state with XOR and shifts, using the standard xorshift32 steps. Begin with the nonzero unsigned state seed.
state = (state XOR (state << 13)) modulo 2^32.state = state XOR (state >> 17), using a logical right shift.state = (state XOR (state << 5)) modulo 2^32.
After all three updates, emit the new unsigned state. Repeat for count consecutive updates and return those values in order. The initial seed itself is not an emitted value. A zero count returns an empty array.
These steps fully define the generator. Complete xorRandomSequence.
Function
xorRandomSequence(seed: long, count: int) → long[]Examples
Example 1
seed = 1count = 5return = [270369,67634689,2647435461,307599695,2398689233]The first transition emits 270369; each following transition uses the previously emitted state.
Example 2
seed = 4294967295count = 2return = [253983,4228382207]The seed has all 32 bits set. Outputs remain unsigned even when bit 31 is set.
Constraints
1 <= seed <= 2^32 - 1.0 <= count <= 2000.- Keep the low 32 bits after each left-shift XOR. Right shift inserts zero bits.
- Return unsigned values as nonnegative 64-bit integers in
[0,2^32 - 1].
More Baseten interview questions
Similar questions at other companies
- CPU Register Bitwise OperationsHeadlands Technologies · PHONE SCREEN
- Set and Clear BitsAMD · PHONE SCREEN
- Clocked Counter with Reset and EnableXilinx · PHONE SCREEN
- Out-of-Order TLV File ReassemblyApplied Intuition · PHONE SCREEN
- Ordered IP FirewallDatabricks · PHONE SCREEN
- CIDR IPv4 Range IteratorOpenAI · PHONE SCREEN
- Three-Speaker Truth ConsistencySusquehanna International Group (SIG) · OA
- Operations to Reduce a Binary String Value to ZeroMarshall Wace · OA
- Repeated Mirror XORMathWorks · OA
- Competitive ForagingAkuna Capital · OA