Solve weighted pick and product except self
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Take-home Project
Quick Answer: This question evaluates algorithm design and data-structure skills, focusing on weighted random selection and array aggregation for product computations within the Coding & Algorithms domain.
Random Pick with Weight (Deterministic Core)
Constraints
- 1 <= len(w) <= 10^4
- 1 <= w[i] <= 10^5
- 1 <= target <= sum(w)
Examples
Input: ([1], 1)
Expected Output: 0
Explanation: Single bucket; any target in [1,1] picks index 0.
Input: ([1, 3], 1)
Expected Output: 0
Explanation: prefix=[1,4]; target 1 <= 1 so index 0.
Input: ([1, 3], 2)
Expected Output: 1
Explanation: prefix=[1,4]; target 2 > 1 but <= 4 so index 1.
Input: ([1, 3], 4)
Expected Output: 1
Explanation: prefix=[1,4]; target 4 lands on the last bucket, index 1.
Input: ([2, 5, 3, 4], 1)
Expected Output: 0
Explanation: prefix=[2,7,10,14]; target 1 <= 2 so index 0.
Input: ([2, 5, 3, 4], 2)
Expected Output: 0
Explanation: target 2 lands exactly on P[0]=2, index 0.
Input: ([2, 5, 3, 4], 7)
Expected Output: 1
Explanation: target 7 lands exactly on P[1]=7, index 1.
Input: ([2, 5, 3, 4], 8)
Expected Output: 2
Explanation: P[1]=7 < 8 <= P[2]=10, so index 2.
Input: ([2, 5, 3, 4], 10)
Expected Output: 2
Explanation: target 10 lands exactly on P[2]=10, index 2.
Input: ([2, 5, 3, 4], 14)
Expected Output: 3
Explanation: Last possible target equals total sum, index 3.
Input: ([5, 5, 5], 6)
Expected Output: 1
Explanation: Equal weights; prefix=[5,10,15]; target 6 > 5 <= 10 so index 1.
Hints
- Convert each weight into a cumulative range. Index i 'owns' the integers (P[i-1], P[i]].
- With prefix sums sorted ascending, finding the owning index for a target is a classic lower-bound binary search: find the first prefix value that is >= target.
- Be careful with the binary-search boundary: use a [lo, hi] window that converges to a single index, moving lo = mid+1 only when prefix[mid] < target.
Product of Array Except Self
Constraints
- 2 <= len(nums) <= 10^5
- -30 <= nums[i] <= 30
- The product of any prefix or suffix of nums fits in a 32-bit integer.
Examples
Input: ([1, 2, 3, 4],)
Expected Output: [24, 12, 8, 6]
Explanation: Standard case: 2*3*4=24, 1*3*4=12, 1*2*4=8, 1*2*3=6.
Input: ([-1, 1, 0, -3, 3],)
Expected Output: [0, 0, 9, 0, 0]
Explanation: Single zero: only the zero's own position gets a nonzero product (-1*1*-3*3=9); all others include the zero.
Input: ([2, 3],)
Expected Output: [3, 2]
Explanation: Two-element minimum: each gets the other.
Input: ([5],)
Expected Output: [1]
Explanation: Single element edge case: product of an empty set is 1.
Input: ([0, 0],)
Expected Output: [0, 0]
Explanation: Two zeros: every position still includes at least one zero, so all outputs are 0.
Input: ([1, 0],)
Expected Output: [0, 1]
Explanation: One zero: index 0 includes the zero (0); index 1 excludes it (1).
Input: ([-2, -3, -4],)
Expected Output: [12, 8, 6]
Explanation: All negatives: -3*-4=12, -2*-4=8, -2*-3=6.
Input: ([1, 1, 1, 1],)
Expected Output: [1, 1, 1, 1]
Explanation: All ones: every product is 1.
Hints
- answer[i] = (product of everything to the left of i) * (product of everything to the right of i). Compute these two pieces separately.
- First pass left-to-right: store in result[i] the running product of all elements strictly before i.
- Second pass right-to-left: multiply result[i] by a running product of all elements strictly after i. This avoids division and naturally handles zeros.