FastPrepUniform to Normal Numbers
Problems/Uniform to Normal Numbers

Uniform to Normal Numbers

Zillow logoZillow● EasyINTERNONSITE INTERVIEW

Updated Dec 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Asked at Zillow in 2019, reported by a candidate for a Machine Learning Engineer Intern role in Seattle, Washington, who did not get an offer. The candidate had three in-person interviews of one hour each, held one after another, and calls it their first on-site. The report lists its questions round by round; in the first round the task was to start from a generator of uniformly spread random numbers and produce numbers that follow a normal distribution. Since the report ties the task to round 1 of those in-person interviews, we list it as an onsite interview.

The report does not say how the uniform numbers reach the code, which method was expected, or what the function returns; the exact form here is ours. Our choices: the function uniformToNormal gets an even-length array u of numbers the uniform generator has already returned, plus a mean mu and a standard deviation sigma; it applies the Box-Muller transform, with the formula given on the page, to each pair u[2k], u[2k + 1], so every answer is fixed; it returns two numbers per pair in that order, scaled by sigma and shifted by mu. The limits (2 to 1,000 numbers, each from 10^-9 to 1, mu from -1,000 to 1,000, sigma from 0.001 to 1,000) and the 10^-9 tolerance are ours as well.

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

Given an array u of n numbers that a uniform random generator has already returned, plus mu and sigma, return n numbers that follow a normal distribution (the bell curve) with mean mu and standard deviation sigma. Uniform means every value between 0 and 1 is equally likely. n is even.

Use the Box-Muller transform, which works on pairs: u[0] with u[1], u[2] with u[3], and so on. For the pair u1 = u[2k] and u2 = u[2k + 1], compute:

  • r = sqrt(-2 * ln(u1)), where ln is the natural logarithm,
  • theta = 2 * pi * u2,
  • z0 = r * cos(theta) and z1 = r * sin(theta).

z0 and z1 are two independent standard normal numbers, which means mean 0 and standard deviation 1. Put them in the returned array out as out[2k] = mu + sigma * z0 and out[2k + 1] = mu + sigma * z1.

A returned number is accepted when it differs from the expected value by at most 10-9 times the larger of 1 and the absolute value of the expected value.

Function

uniformToNormal(u: double[], mu: double, sigma: double) → double[]

Examples

Example 1

u = [0.5,0.875,0.25,0.375]mu = 0.0sigma = 1.0return = [0.8325546111576978,-0.8325546111576978,-1.1774100225154747,1.1774100225154747]

The first pair (0.5, 0.875) gives r about 1.17741 and theta = 1.75 * pi, so z0 and z1 are about 0.83255 and -0.83255. The second pair (0.25, 0.375) gives r about 1.66511 and theta = 0.75 * pi, so about -1.17741 and 1.17741, returned as they are since mu = 0 and sigma = 1.

Example 2

u = [0.5,0.875]mu = 10.0sigma = 2.0return = [11.665109222315396,8.334890777684604]

This is the first pair of Example 1, so z0 and z1 are about 0.83255 and -0.83255. Then out[0] = 10 + 2 * z0 is about 11.66511 and out[1] = 10 + 2 * z1 is about 8.33489.

Example 3

u = [1.0,0.3]mu = -5.0sigma = 3.0return = [-5.0,-5.0]

Here u1 = 1, so ln(u1) = 0 and r = 0. Both z0 and z1 are 0 whatever u2 is, and both returned numbers equal mu = -5.

Constraints

  • 2 <= u.length <= 1000, and u.length is even
  • 10-9 <= u[i] <= 1
  • -1000 <= mu <= 1000
  • 0.001 <= sigma <= 1000

More Zillow Onsite Interview questions

  • Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
  • Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
  • Spiral MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
  • First Subarray with the Largest SumOA · ONSITE INTERVIEW · Seen Aug 2021
  • Count Duplicate CharactersONSITE INTERVIEW · Seen Oct 2020
  • Symmetric TreeONSITE INTERVIEW · Seen Oct 2020
  • Decision Tree Classifier from ScratchONSITE INTERVIEW · Seen Nov 2019
  • Check Each Word in a Character GridONSITE INTERVIEW · Seen Oct 2018
  • Find Median from Data StreamPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2018
  • Is SubstringONSITE INTERVIEW · Seen Nov 2017

Similar questions at other companies

  • Streaming Entropy, Part 1: Batch EntropyOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
  • Confusing Bib NumbersVerily · ONSITE INTERVIEW
  • Plus OneDruva · ONSITE INTERVIEW
  • Rotate a Flat Image ClockwiseGoogle · ONSITE INTERVIEWGoogle interview questions
  • Matrix MultiplicationeBay · ONSITE INTERVIEWeBay interview questions
  • Maximum Product of Three IntegersPelatro · ONSITE INTERVIEW
  • Exact-Sixty Dice Game ValueJane Street · ONSITE INTERVIEWJane Street interview questions
  • Find the Missing Number from 1 to nHyperVerge · ONSITE INTERVIEW
  • Alternating Index Square SumAccenture · ONSITE INTERVIEWAccenture interview questions
  • Moving Cost by Volume and CategoryWayfair · ONSITE INTERVIEWWayfair interview questions
All Zillow interview questionsSee Zillow hiring insights
Saved
class Solution {
    public double[] uniformToNormal(double[] u, double mu, double sigma) {
        // Write your code here.
    }
}
Checking account…