Answer four string/array/battery coding questions
MetaSoftware Engineer · OnsiteLast updated
This set of four problems evaluates algorithmic competencies including ASCII string processing and character classification, array transformation and operation-count reasoning, cost-minimization for enforcing monotonicity under only-increase constraints, and time-based simulation of resource scheduling with recharge cycles.
You are given four independent coding questions.
Problem A — Uppercase vs lowercase count difference
Given a string s (ASCII), count how many characters are uppercase letters ('A'..'Z') and how many are lowercase letters ('a'..'z'). All other characters are ignored.
Return: uppercaseCount - lowercaseCount.
Constraints: 1 ≤ |s| ≤ 1e5.
Problem B — Repeated left-to-right subtraction until all zeros
Given an array nums of n non-negative integers.
Repeat the following operation until all elements are 0:
- Let
ibe the smallest index such thatnums[i] > 0. If no suchiexists, stop. - Let
x = nums[i]. - Starting from
j = iand moving right, subtractxfrom eachnums[j]whilej < n,nums[j] > 0, andnums[j] >= x. - Stop the operation at the first index
jwhere one of those conditions fails (i.e., you “hit” an element strictly smaller thanx, or a0, or the end of the array).
Each application of steps (1)–(4) counts as one operation.
Task: Return the number of operations needed to make all elements 0.
Constraints: 1 ≤ n ≤ 2e5, 0 ≤ nums[i] ≤ 1e9.
Problem C — Minimum total increments to make array monotone
Given an integer array a of length n. You may only increase elements. Increasing a[i] by d ≥ 0 costs d operations.
You want to obtain an array b such that:
b[i] >= a[i]for alli, and- either
bis non-decreasing (b[i] <= b[i+1]) orbis non-increasing (b[i] >= b[i+1]).
Return: the minimum possible total cost sum_i (b[i] - a[i]) over the better of the two choices (make it non-decreasing or non-increasing).
Constraints: 1 ≤ n ≤ 2e5, |a[i]| ≤ 1e9. Use 64-bit for sums.
Problem D — Phone batteries with recharge cycles: drains by time T
You have n batteries. Battery i has:
- capacity
cap[i](minutes of phone runtime when fully charged) - recharge time
rec[i](minutes needed to recharge from empty back to full)
Rules:
- At time
0, all batteries are fully charged and available. - The phone uses one battery at a time.
- When a battery is used, it is used continuously until it is fully drained (unless the overall time limit
Tis reached first). - When a battery is fully drained at time
t, it immediately starts recharging and becomes available again (fully charged) at timet + rec[i]. - Whenever the phone needs a new battery, it selects among currently available (fully charged) batteries using this deterministic tie-break:
- choose the available battery with the smallest index.
- If no battery is available, the phone waits (no battery in use) until the earliest time some battery becomes available, then continues.
Task: Given cap[], rec[], and a wall-clock time limit T, return how many times a battery becomes fully drained during the time interval [0, T].
Constraints (for implementation): 1 ≤ n ≤ 2e5, 1 ≤ cap[i], rec[i] ≤ 1e9, 0 ≤ T ≤ 1e12. Use 64-bit arithmetic.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.