FastPrepGenerate Unique Random Numbers
Problems/Generate Unique Random Numbers

Generate Unique Random Numbers

Google logoGoogle● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A firsthand Google internship report names Fisher-Yates, unique values from a through b, a fixed running time, and bingo-card follow-ups. The report did not define a way to repeat the random numbers. So we added a fixed seeded generator. Our version is about 90% the same as the reported question.

Special thanks: Thanks a lot to Rachel for carrying this one!! 🍊

Build the inclusive array [a, a + 1, ..., b], shuffle it in place with the Fisher-Yates algorithm, and return its first n values. The returned values must therefore be distinct and each must lie between a and b, inclusive.

To make the judged output reproducible, use this exact pseudorandom-number rule. Let state = seed. For every Fisher-Yates iteration from i = b - a down to 1, first set state = (1103515245 * state + 12345) mod 2^31, then set j = state mod (i + 1) and swap positions i and j. Return the first n shuffled values.

The running time must be deterministic with respect to the size of the inclusive range.

Function

generateUniqueNumbers(a: int, b: int, n: int, seed: int) → int[]

Examples

Example 1

a = 1b = 5n = 3seed = 7return = [1,3,4]

The seeded Fisher-Yates swaps produce the shuffled prefix [1, 3, 4]. All three values are distinct and lie in the inclusive range.

Example 2

a = 10b = 13n = 4seed = 0return = [10,12,13,11]

Because n equals the range size, the full seeded permutation is returned.

Example 3

a = -2b = 2n = 1seed = 42return = [-1]

Only the first element of the seeded permutation is requested.

Constraints

  • -10^6 <= a <= b <= 10^6
  • 1 <= b - a + 1 <= 200000
  • 1 <= n <= b - a + 1
  • 0 <= seed <= 2147483647

More Google Onsite Interview questions

  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
  • Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Filesystem Entity Total SizeONSITE INTERVIEW · Seen Nov 2026
  • Longest Subarray Within an Absolute-Difference LimitONSITE INTERVIEW · Seen Nov 2026
  • Reach a Destination Through Scheduled FlightsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Expected Value of Modified DiceJane Street · ONSITE INTERVIEWJane Street interview questions
  • Reservoir Sampling with Recorded DrawsMeta · ONSITE INTERVIEWMeta interview questions
  • Group-Relative Policy AdvantagesAmazon · ONSITE INTERVIEW
  • Reservoir Sampling with Recorded DrawsStackAdapt · ONSITE INTERVIEWStackAdapt interview questions
  • Sliding-Window Averages as Reduced FractionsCoinbase · ONSITE INTERVIEWCoinbase interview questions
  • Apply a Sequence of Array RotationsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • K-Means Delivery ZonesDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
  • Moving Cost by Volume and CategoryWayfair · ONSITE INTERVIEWWayfair interview questions
  • Tensor-Parallel MLP GradientsOpenAI · ONSITE INTERVIEWOpenAI interview questions
  • Vectorized K-Means AssignmentsSambaNova Systems · ONSITE INTERVIEWSambaNova Systems interview questions

Interview reports that mention this question

  • Google, Datadog, LinkedIn SWE Intern Onsites: 1 Experience

Google interview experiences

  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
  • Google AI Coding Interview and AI OA: 2 Experiences
  • Google SWE Intern Interview Results Timeline: 13 Experiences
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int[] generateUniqueNumbers(int a, int b, int n, int seed) {
      // Write your code here.
    }
}
Checking account…