← 返回博客列表
Meta

Meta / CodeSignal OA (4 Questions) Review: Q1 Digit Sum, Q2 Drone Delivery Simulation, Q3 Battery Scheduling, Q4 Dynamic Two-Sum

2025-08-20

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:

Related reading:


Q1 — Minimum Sum After One Operation

Problem

Given an array of positive integers nums, you may perform at most one operation:

Return the minimum possible array sum after 0 or 1 operation.

Key idea (classic warm-up)

This is a greedy “compute the delta” problem:

Complexity

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:

  1. From the current cargo location, walk with the cargo to the nearest station
  2. Launch a drone from that station, moving the cargo up to 10 units toward target
  3. If the cargo is not delivered yet, you walk without the cargo to pick it up (this distance does not count)
  4. Repeat until the cargo reaches target

Return the total distance you walked while carrying the cargo.

Common pitfalls

Robust simulation

Track only one variable: cargoPos.

Each round:

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:

Rules:

Simulation state

Let nextFull[i] be the next time when battery i becomes fully charged.

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:

Constraints:

Correct approach: frequency maps

Maintain:

Update:

Query:

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

If you’re preparing for Meta / CodeSignal OA, treat simulation questions like engineering tasks:

Need real interview questions? Contact WeChat: Coding0201 to get the questions.