Four short array and string exercises
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Meta's AI-enabled coding round, last asked Dec 2025. It tests arrays, strings and simulation.
Four short, independent problems in one round. Solve each one, state its complexity, and test it.
Part 1: Uppercase minus lowercase
Given an ASCII string s, count its uppercase letters (A–Z) and its lowercase letters (a–z); ignore every other character. Return the uppercase count minus the lowercase count.
-
case_difference(s: str) -> int, with1 <= len(s) <= 10^5.
"AbC!z" -> 0 # 2 upper, 2 lower
"Hello, World" -> -6 # 2 upper, 8 lower
"123" -> 0
Part 2: Left-to-right subtraction until all zeros
Given an array nums of non-negative integers, repeat one operation until every element is 0. An operation: take the leftmost index i with nums[i] > 0 and let x = nums[i]. Moving right from i, subtract x from each element while the element is positive and at least x; stop at the first element that is 0, smaller than x, or past the end. Return the number of operations.
-
count_operations(nums: list[int]) -> int, with1 <= n <= 2 * 10^5and0 <= nums[i] <= 10^9.
[1, 2, 3] -> 3 # [0,1,2], [0,0,1], [0,0,0]
[3, 1, 2] -> 3 # [0,1,2], [0,0,1], [0,0,0]
[2, 4, 1, 3] -> 4 # [0,2,1,3], [0,0,1,3], [0,0,0,2], [0,0,0,0]
Each operation empties the leftmost positive element, so there are at most n operations, but a direct simulation can still cost O(n^2) time. Look for a faster way to count them.
Part 3: Cheapest way to make an array monotone by increasing
Given an integer array a, you may only increase elements, and raising an element by d costs d. Return the minimum total cost to make the array either non-decreasing or non-increasing, whichever is cheaper.
-
min_monotone_cost(a: list[int]) -> int, with1 <= n <= 2 * 10^5and|a[i]| <= 10^9; use 64-bit sums.
[3, 1, 2] -> 1 # raise 1 to 2: [3, 2, 2] is non-increasing
[1, 2, 3] -> 0
[5, 1, 4, 2] -> 3 # [5, 4, 4, 2]
Part 4: Phone batteries that recharge
There are n batteries; battery i runs the phone for cap[i] minutes and takes rec[i] minutes to recharge from empty. At time 0 all are full. The phone uses one battery at a time until it is empty; a drained battery starts recharging at once and is full again rec[i] minutes later. When the phone needs a battery it takes the full battery with the smallest index; if none is full, it waits until one is. Return how many times a battery is fully drained within [0, T].
-
count_drains(cap: list[int], rec: list[int], T: int) -> int, with1 <= n <= 2 * 10^5,1 <= cap[i], rec[i] <= 10^9,0 <= T <= 10^12.
cap = [5], rec = [5], T = 20 -> 2 # drains at 5 and 15; the next would be at 25
cap = [3, 4], rec = [10, 1], T = 10 -> 2 # 0 drains at 3, 1 drains at 7, 1 runs again from 8 to 12
cap = [2, 3], rec = [4, 1], T = 12 -> 4 # drains at 2, 5, 8 and 11
Clarifying questions
- Part 1: are non-ASCII letters possible, and should they count?
- Part 2: should I return the number of operations, or the sum of the
xvalues subtracted? - Part 4: does a drain at exactly time
Tcount? (Here it does.) - Part 4: is
Tlarge enough that a step-by-step simulation is too slow?
What a strong answer covers
- Part 1: one pass,
O(n). - Part 2: a correct simulation first, an argument for why it ends after at most
noperations, then a faster count, for example by tracking which elements an operation can still reach. - Part 3: a prefix maximum for the non-decreasing case and a suffix maximum for the non-increasing case,
O(n)each; take the smaller cost. - Part 4: an event simulation with a min-heap of recharge times and a min-heap of available indices, and a way to skip ahead when the schedule becomes periodic so a huge
Tis fast. - Tests at the edges of each part: empty or all-zero arrays, single elements,
T = 0.
Follow-ups
- Part 2: return the sum of the
xvalues subtracted instead of the number of operations. - Part 3: allow both increases and decreases.
- Part 4: the phone picks the battery with the most capacity instead of the smallest index.
Where it's asked
- Meta · AI-enabled coding · Last asked Dec 202560 min · Several progressive tasks on a starter codebase in CoderPad with an AI assistant panel; usually two rounds · The first task usually without AI, later tasks with it
What it tests
arrays · strings · simulation
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
