Level: Mid-Level
Round: Online Assessment · Type: Coding · Difficulty: 7/10 · Duration: 120 min · Interviewer: Neutral
Topics: Arrays, Greedy Algorithms
Location: San Francisco Bay Area
Interview date: 2025-12-27
Question: Given an integer array, determine the minimum non-negative integer x required to make the array non-decreasing by incrementing a non-adjacent subset of elements by x at most once.
Question: Implement a function to return the minimum operations to ensure a string contains no segments of exactly m consecutive '0's. In one operation, select a contiguous segment of length k and make every bit in this segment '1'.
For the first question, I was asked to find the minimum increment value to make an array non-decreasing by incrementing non-adjacent elements. I implemented a solution that first checks for consecutive descents, which would make it impossible to solve. Then, I determined the lower and upper bounds for the increment value 'x' based on the descents and ascents in the array. Finally, I returned the lower bound if it's within the valid range, otherwise -1.
The coding question I got was: ` from typing import List
def getMinimumIncrement(arr: List[int]) -> int: """ Q1: One operation max: - choose non-adjacent indices - add same non-negative x to all chosen Return minimum x to make arr non-decreasing, else -1. """ n = len(arr) if n <= 1: return 0
lower = 0
upper = float("inf")
for i in range(n - 1):
if arr[i] > arr[i + 1]:
# Two consecutive descents force adjacent selected elements -> impossible
if i + 1 < n - 1 and arr[i + 1] > arr[i + 2]:
return -1
# Need x >= arr[i] - arr[i+1] for this descent
lower = max(lower, arr[i] - arr[i + 1])
# If there is a next pair, selected (i+1) must not exceed arr[i+2]
# so x <= arr[i+2] - arr[i+1]
if i + 1 < n - 1:
upper = min(upper, arr[i + 2] - arr[i + 1])
return lower if lower <= upper else -1
`
For the second question, I needed to find the minimum operations to ensure that a given string does not contain segments of exactly 'm' consecutive '0's. My approach involved iterating through the string, tracking the count of consecutive zeros, and when the count reaches 'm', incrementing the operation count and setting a segment of length 'k' to '1'.
The coding question I got was: ` def getMinOperations(s: str, m: int, k: int) -> int: """ Q2 (following the example, i.e. no run of 0s with length >= m): One operation: pick a segment of length k, set all bits there to '1'. Return minimum operations. """ n = len(s) ops = 0 i = 0 zero_run = 0 last_start = n - k
while i < n:
if s[i] == '1':
zero_run = 0
i += 1
continue
zero_run += 1
if zero_run == m:
ops += 1
# Rightmost valid start that still covers current index i
start = i if i <= last_start else last_start
# Entire operated segment is now '1'
i = start + k
zero_run = 0
else:
i += 1
return ops
`