Level: Junior-Level
Round: Online Assessment · Type: Coding · Difficulty: 7/10 · Duration: 60 min · Interviewer: Unfriendly
Topics: Greedy Algorithms, Bit Manipulation, Sliding Window, Hash Table, Monotonic Stack
Location: San Francisco, CA
Interview date: 2026-01-21
I completed an online assessment with three coding questions on HackerRank.
Question 1: Minimize operations to reduce n to 0 using powers of 2.
Question 2: Find the shortest subarray containing at least k distinct integers.
Question 3: Calculate the final price with discounts based on the first smaller price to the right.
The online assessment consisted of three questions on HackerRank. The questions involved a full-screen mode but did not require a camera.
Question 1:
Minimize operations to convert a positive integer n to 0. In each operation, you can either add or subtract 2^i (where i >= 0).
The optimal strategy is a greedy approach using the signed binary representation (Non-Adjacent Form - NAF). Steps:
Special cases: n == 1, directly subtract 1; n == 3, subtracting 1 is more intuitive.
Question 2:
Given an array arr and an integer k, find the length of the shortest subarray that contains at least k distinct integers. Return -1 if no such subarray exists.
I used a sliding window/two-pointer technique with a counting hash table. Steps:
left and right pointers.dict to record the frequency of each value within the window, and use len(dict) to represent the number of distinct elements.right pointer to the right, adding arr[right] into count.len(dict) >= k, attempt to contract the left pointer:
res = min(res, right - left + 1)left by 1Question 3:
For each index i in prices, the discount is the price at the first index j > i such that prices[j] <= prices[i]. If no such j exists, there is no discount and the original price is used.
Output:
I used a monotonic stack (find the first element to the right that is <= the current element).
Iterate from right to left, maintaining a stack of indices representing candidate discounts in non-decreasing price order. When processing index i:
prices[stack[-1]] > prices[i], pop the stack (as these prices cannot be valid discounts).i as having no discount, and add prices[i] to the total price.prices[stack[-1]], and add prices[i] - prices[stack[-1]] to the total price.i onto the stack.Reverse the list of indices without discounts to produce ascending order.