ToptalSeptember 2026
Interview question
Write a Python function solution(X, Y) that takes distributions of two random variables X and Y and returns the possible values of Z = X² - Y, ordered from least probable to most probable. Break equal-probability ties by smaller value first. Each input is an integer array alternating a value between -1000 and 1000 and its probability scaled from 0 to 100. For X = [-1, 50, 1, 50] and Y = [-1, 40, 1, 60], return [2, 0]. For X = [2, 50, 0, 25, 1, 25] and Y = [-1000, 100], return [1000, 1001, 1004]. Use only standard built-in Python libraries.