FastPrepImplement a Seeded Pseudorandom Number Generator
Problems/Implement a Seeded Pseudorandom Number Generator

Implement a Seeded Pseudorandom Number Generator

Morgan Stanley logoMorgan Stanley● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Morgan Stanley onsite interview for a full-time role. The author only says to implement a random number generator algorithm, and says they did not find an effective solution. Here we use a multiplicative congruential formula with a fixed outcome. We chose its constants, the seed and count interface, and the exact output rules. We do not claim it is the algorithm from the interview, or that it is truly random for security.

Our version is about 50% the same as the reported question.

Implement the following deterministic seeded pseudorandom generator. Start with state=seed. For each requested output, first update the state using state=(48271*state) mod 2147483647, then append that updated state.

Return exactly count updated states in order. Do not include the initial seed as an output, and return an empty array when count is 0. Each call starts fresh from its supplied seed and retains no state for later calls.

Use exact integer arithmetic. The seed is positive and smaller than the modulus, and every returned state must remain in that same range. Do not replace the recurrence with a built-in random library, system clock or entropy source. The exercise specifies reproducible pseudorandom states and makes no distribution or cryptographic guarantee.

Function

generatePseudorandom(seed: int, count: int) → int[]

Examples

Example 1

seed = 1count = 3return = [48271,182605794,1291394886]

Starting from seed 1, the first update is 48271. Applying the recurrence twice more gives 182605794 and 1291394886. The original seed 1 is not included in the three returned outputs.

Example 2

seed = 44488count = 2return = [2147480248,1983410518]

The first update from seed 44488 is 2147480248, since 48271*44488 equals 2147483647-3399. The second returned value applies the recurrence once more to that updated state.

Constraints

  • For this exercise, assume 1 <= seed <= 2147483646.
  • For this exercise, assume 0 <= count <= 1000.

More Morgan Stanley Onsite Interview questions

  • Generate Strings of Length at Least ThreeONSITE INTERVIEW · Seen Sep 2026
  • Maximum Concurrent ProcessesOA · ONSITE INTERVIEW · Seen Sep 2026
  • Kth Largest Array ElementONSITE INTERVIEW · Seen Jul 2026
  • Run-Length Encode Consecutive Letter RunsONSITE INTERVIEW · Seen Jul 2026
  • Maximum Edge Removals for Two TravelersONSITE INTERVIEW · Seen Oct 2025
  • Remove K DigitsONSITE INTERVIEW · Seen Oct 2025
  • Count Ideal ArraysONSITE INTERVIEW · Seen Aug 2023
  • Distinct Palindrome PartitionsONSITE INTERVIEW · Seen Aug 2023
  • LFU Cache OperationsONSITE INTERVIEW · Seen Aug 2023
  • All-Pairs Shortest PathsOA · ONSITE INTERVIEW · Seen Feb 2022

Similar questions at other companies

  • Babysitter Quote with Hourly RatesUpstart · ONSITE INTERVIEWUpstart interview questions
  • Expected Value of Modified DiceJane Street · ONSITE INTERVIEWJane Street interview questions
  • Fractional Share Inventory FlatteningRobinhood · PHONE SCREEN · ONSITE INTERVIEWRobinhood 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
  • Iterate Canonical IPv6 AddressesOpenAI · ONSITE INTERVIEWOpenAI interview questions
  • Nth Staged License PlateGoogle · ONSITE INTERVIEWGoogle interview questions
  • UCB1 Multi-Armed Bandit TraceWayfair · ONSITE INTERVIEWWayfair interview questions
  • Rasterize a Circle with Integer PixelsPure Storage · ONSITE INTERVIEWPure Storage interview questions

Interview reports that mention this question

  • Morgan Stanley Apprenticeship and Tech Interviews: 6 Experiences

Morgan Stanley interview experiences

  • Morgan Stanley New Grad Technology Analyst: 2 Experiences
  • Morgan Stanley Intern Oldest Loops: 5 Experiences (Part 5 of 5)
  • Morgan Stanley Intern OOP Loops: 5 Experiences (Part 4 of 5)
  • Morgan Stanley Intern Earlier Loops: 5 Experiences (Part 3 of 5)
  • Morgan Stanley Intern Tech Rounds: 5 Experiences (Part 2 of 5)
All Morgan Stanley interview questionsSee Morgan Stanley hiring insights
Saved
class Solution {
    public int[] generatePseudorandom(int seed, int count) {
        // Write your code here.
    }

}
Checking account…