Katkat

Answer four string/array/battery coding questions

MetaSoftware Engineer · Onsite

Last 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:

  1. Let i be the smallest index such that nums[i] > 0. If no such i exists, stop.
  2. Let x = nums[i].
  3. Starting from j = i and moving right, subtract x from each nums[j] while j < n, nums[j] > 0, and nums[j] >= x.
  4. Stop the operation at the first index j where one of those conditions fails (i.e., you “hit” an element strictly smaller than x, or a 0, 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 all i, and
  • either b is non-decreasing (b[i] <= b[i+1]) or b is 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 T is reached first).
  • When a battery is fully drained at time t, it immediately starts recharging and becomes available again (fully charged) at time t + 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.