These CodeSignal-style OAs share the same flavor: the algorithm is not necessarily hard, but the rules are detailed and the implementation steps are long. The key is not to “over-optimize in your head”, but to translate every constraint into code.
This post reviews Q1 → Q4 with:
- What the problem is asking
- The most robust approach to implement
- Common pitfalls
- Reusable code templates
Related reading:
Q1 — Minimum Sum After One Operation
Problem
Given an array of positive integers nums, you may perform at most one operation:
- Choose an index
i - Replace
nums[i]with the sum of its digits
Return the minimum possible array sum after 0 or 1 operation.
Key idea (classic warm-up)
This is a greedy “compute the delta” problem:
- Let the original sum be
S - For each
x, computedigitSum(x) - New sum is
S - x + digitSum(x) - Take the minimum across all choices (including doing nothing)
Complexity
- Time:
O(n * log10(max(nums))) - Space:
O(1)
Python template
def digit_sum(x: int) -> int:
s = 0
while x > 0:
s += x % 10
x //= 10
return s
def min_sum_after_one_operation(nums: list[int]) -> int:
S = sum(nums)
ans = S
for x in nums:
ans = min(ans, S - x + digit_sum(x))
return ans
Q2 — Delivery System Using Drones
Problem
Given a target position and a list of stations (1D coordinates), you repeatedly follow the exact process described in the prompt:
- From the current cargo location, walk with the cargo to the nearest station
- Launch a drone from that station, moving the cargo up to 10 units toward
target - If the cargo is not delivered yet, you walk without the cargo to pick it up (this distance does not count)
- Repeat until the cargo reaches
target
Return the total distance you walked while carrying the cargo.
Common pitfalls
- You must sort
stationsfirst - Only “walk with cargo” counts; “walk empty-handed” does not
- The rule is nearest station, not “the next station in the direction”
Robust simulation
Track only one variable: cargoPos.
Each round:
- Find nearest station
s ans += abs(cargoPos - s)and setcargoPos = s- Drone moves cargo toward
targetby at most 10
Python template
import bisect
def nearest_station(pos: int, stations: list[int]) -> int:
# stations must be sorted
j = bisect.bisect_left(stations, pos)
cand = []
if j < len(stations):
cand.append(stations[j])
if j - 1 >= 0:
cand.append(stations[j - 1])
# If tie-break is not specified, prefer smaller coordinate to be stable
cand.sort(key=lambda s: (abs(s - pos), s))
return cand[0]
def delivery_distance_with_cargo(target: int, stations: list[int]) -> int:
stations.sort()
cargo = 0
ans = 0
while cargo != target:
s = nearest_station(cargo, stations)
ans += abs(cargo - s)
cargo = s
if target >= cargo:
cargo = min(target, cargo + 10)
else:
cargo = max(target, cargo - 10)
# If not at target yet: walk empty-handed to pick up (not counted)
return ans
Q3 — Battery Usage Simulation
Problem
You want to run a device for total time t.
You have multiple batteries:
capacity[i]: how long it can run when fully chargedrecharge[i]: time to fully recharge from 0
Rules:
- You can only use a fully charged battery
- Once a battery is exhausted, it starts recharging immediately
- If you need a battery but none is fully charged, return
-1 - A subtle but common requirement: if you overshoot
t(run longer thantusing the last battery), it should be considered invalid (return-1). A safe approach is to require the sum to hittexactly.
Simulation state
Let nextFull[i] be the next time when battery i becomes fully charged.
- Initially all are full:
nextFull[i] = 0 - Maintain current time
cur - At time
cur, any battery withnextFull[i] <= curis available - After using battery
i: push it back to future with timecur + recharge[i]
A practical greedy heuristic is to always pick the available battery with the largest capacity.
Python template (heaps)
import heapq
def min_batteries_to_run(t: int, capacity: list[int], recharge: list[int]) -> int:
n = len(capacity)
# future: (time_when_full, idx)
future = [(0, i) for i in range(n)]
heapq.heapify(future)
# ready: (-capacity, idx)
ready = []
cur = 0
used = 0
while cur < t:
while future and future[0][0] <= cur:
_, i = heapq.heappop(future)
heapq.heappush(ready, (-capacity[i], i))
if not ready:
return -1
cap_neg, i = heapq.heappop(ready)
cap = -cap_neg
cur += cap
used += 1
if cur > t:
return -1
heapq.heappush(future, (cur + recharge[i], i))
return used
Q4 — Primary + Secondary Arrays with Operations
Problem
Given arrays primary and secondary and a list of operations:
[0, index, val]: updatesecondary[index] = val[1, targetSum]: query the number of pairs(i, j)such thatprimary[i] + secondary[j] == targetSum
Constraints:
primaryis fixedsecondarychanges frequently
Correct approach: frequency maps
Maintain:
freqA = Counter(primary)(fixed)freqB = Counter(secondary)(dynamic)
Update:
- decrement old value in
freqB - increment new value in
freqB
Query:
- iterate distinct values
ainfreqA - accumulate
freqA[a] * freqB[targetSum - a]
Python template
from collections import Counter
def process_operations(primary: list[int], secondary: list[int], operations: list[list[int]]) -> list[int]:
freqA = Counter(primary)
freqB = Counter(secondary)
ans = []
for op in operations:
if op[0] == 0:
idx, val = op[1], op[2]
old = secondary[idx]
freqB[old] -= 1
if freqB[old] == 0:
del freqB[old]
secondary[idx] = val
freqB[val] += 1
else:
target = op[1]
total = 0
for a, cntA in freqA.items():
total += cntA * freqB.get(target - a, 0)
ans.append(total)
return ans
Takeaway: this OA set tests execution, not fancy tricks
- Q1: compute deltas, don’t overcomplicate
- Q2: follow the process exactly; simulation beats intuition
- Q3: state machine (ready vs charging); edge cases matter most
- Q4: fixed + dynamic two-sum pattern; frequency maps are the core
If you’re preparing for Meta / CodeSignal OA, treat simulation questions like engineering tasks:
- write down the steps first
- define state variables
- implement tie-breaks and edge cases
Need real interview questions? Contact WeChat: Coding0201 to get the questions.