Uniform to Normal Numbers
Updated
Problem statement
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)), wherelnis the natural logarithm,theta = 2 * pi * u2,z0 = r * cos(theta)andz1 = 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, andu.lengthis even10-9 <= u[i] <= 1-1000 <= mu <= 10000.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