Coding questions · Meta
Answer four string/array/battery coding questions
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.
Uppercase Minus Lowercase Count
Count ASCII uppercase and lowercase letters and return uppercase_count - lowercase_count.
Examples
Example 1
Input: ('AbC!z',)
Output: 0
Explanation: Three uppercase/lowercase letters mixed with punctuation.
Example 2
Input: ('123',)
Output: 0
Explanation: Ignore non-letters.
Hints
- Pick a representation that makes the requested operation direct.
- Handle empty inputs and boundary cases first.
Repeated Left-to-Right Subtraction Count
Simulate the specified deterministic subtraction process until all entries are zero and return the number of operations.
Examples
Example 1
Input: ([1,2,3],)
Output: 3
Explanation: Each layer subtracts left to right.
Example 2
Input: ([3,1,2],)
Output: 3
Explanation: Stops at smaller positive value.
Hints
- Pick a representation that makes the requested operation direct.
- Handle empty inputs and boundary cases first.
Minimum Increments to Make Array Monotone
Only increase elements. Return the lower cost to make the array non-decreasing or non-increasing.
Examples
Example 1
Input: ([3,1,2],)
Output: 1
Explanation: Can increase to non-decreasing or non-increasing.
Example 2
Input: ([1,2,3],)
Output: 0
Explanation: Already non-decreasing.
Hints
- Pick a representation that makes the requested operation direct.
- Handle empty inputs and boundary cases first.
Battery Drains by Time Limit
Simulate fully charged batteries with recharge times and smallest-index selection. Return how many full drains happen within [0,T].
Examples
Example 1
Input: ([5], [5], 20)
Output: 2
Explanation: One battery cycles with waits.
Example 2
Input: ([3,4], [10,1], 10)
Output: 2
Explanation: Smallest available index first.
Hints
- Pick a representation that makes the requested operation direct.
- Handle empty inputs and boundary cases first.