Evaluating LLMs on Complex Algorithms and State-Replay Tasks

    A neutral evaluation exposed a common gap: when a task combines complex state, edge cases, multi-objective optimization, and implicit specifications, looking only at code that “seems to run” is not enough. This article explains how to assess engineering reasoning on a periodic time-window multi-objective shortest path and an out-of-order idempotent log replay, covering formal specs, explainable pitfalls, differential and property tests, and complexity checks.

    Evaluating LLMs on Complex Algorithms and State-Replay Tasks

    In one neutral evaluation, the reviewers first used a general-purpose conversational model to generate two hard engineering problems, then had the model under test answer them and reviewed the results. The tested model performed worst on a multi-objective shortest-path problem with periodic time windows, and only moderately on an out-of-order idempotent log-replay problem. The scores themselves matter less than the broader issue they reveal: when a task combines complex state, boundary conditions, multi-objective optimization, and implicit specifications, how should we objectively evaluate an LLM’s engineering reasoning instead of only checking whether the code “looks like it runs”?

    1. Why these two problems are representative

    The first problem is a directed-graph shortest path with periodic departure constraints. It must return the earliest arrival time, the minimum cost among paths with that earliest arrival, and, when both arrival time and cost are equal, the lexicographically smallest node path. Each edge has six fields: (u, v, phase, period, duration, cost). A departure time is legal only if (depart + phase) % period == 0; otherwise the traveler must wait until the next legal instant. The scale reaches 2 * 10^5 nodes and 3 * 10^5 edges, with times and periods up to 10^9 or even 10^18.

    The second problem is idempotent replay of out-of-order logs and snapshot visibility. Each log entry has a timestamp, actor, idempotency key, operation type, and payload. Events must be replayed in ascending order of (ts, actor, op_id). Under the same actor, a given idempotency key may take effect only once. add, move, and delete create an object, transfer ownership, and delete an object, respectively. snapshot records the visible state of selected objects at that moment and does not modify system state.

    The two problems represent two typical engineering skills: designing a correct algorithm under complex constraints while handling multiple objectives and boundaries, and implementing verifiable business rules in a state machine with idempotent semantics. Neither is a single-point knowledge quiz. Both test specification understanding, data structures, complexity control, edge-case coverage, and self-verification together.

    2. Principles for evaluation design

    2.1 Specifications must be formal

    Natural-language statements are easy to misread. Does “earliest arrival” include waiting at the start? Does “minimum cost” count only edge costs? How should “lexicographically smallest node path” compare paths of different lengths? Does a snapshot record an object’s value, its owner, or both? An evaluation should state a formal definition before the model implements the solution. Otherwise a failure may come from an ambiguous prompt rather than from the model’s ability.

    2.2 Hidden pitfalls must be explainable

    Hidden pitfalls are not traps for their own sake; they are real engineering boundaries. Examples include many equivalent paths when period = 1 and cost = 0; whether s == t should yield a single-node path or an empty path; multi-edges that must each be relaxed; integer times that must not use floating point; later logs with a repeated op_id under the same actor that must be ignored; and identical op_id values used by different actors that must not interfere. Each pitfall should map to an explicit test case and an expected behavior.

    2.3 Prefer differential and property tests over single samples

    A single sample only shows that a solution happened to pass. A more reliable method is a small random graph or random log generator, checked against a naive reference implementation. For the graph algorithm, check monotonic arrival times, non-negative costs, path validity, and legal wait times. For log replay, check idempotency, sort stability, read-only snapshots, and consistent permission checks.

    2.4 Complexity matters as much as correctness

    The constraints already imply a target complexity. A graph algorithm that stores a full path at every node and compares path strings may degrade on large inputs. Log replay that linearly scans history for every event may also time out. The evaluation should ask the model to state its complexity and check that the implementation matches the constraints.

    3. TC1: Multi-objective shortest paths under periodic time windows

    3.1 Key difficulties

    This is not an ordinary shortest path. Edge weights are time-dependent: leaving node u at time T first requires the next legal departure. If a legal departure satisfies depart ≡ -phase (mod period), the wait is:

    1. delta = ((-phase - T) % period + period) % period
    2. depart = T + delta
    3. arrival = depart + duration

    Modulo must be integer arithmetic, not floating point. Because period can reach 10^9 and T can reach 10^18, the language’s big integers or 64-bit integers must be sufficient.

    Objective order is also critical: minimize arrival time first, then total cost, and only then compare path lexicographic order. Many implementations optimize cost first or blend cost and time into one weight, which is wrong. The correct state key is (arrival, cost), with lexicographic path comparison only when both are equal.

    3.2 Algorithm outline

    Because the wait function is non-decreasing and duration >= 1, arrival time strictly increases along a path, so a Dijkstra variant can be used. Each node keeps the current best (arrival, cost) and the corresponding path information. When relaxing an edge, compute:

    1. candidate_arrival = arrival[u] + wait + duration
    2. candidate_cost = cost[u] + edge.cost

    Update if the candidate arrival is smaller, or if the arrival is equal and the cost is smaller. If both are equal, compare the candidate path with the current path and keep the lexicographically smaller one.

    Efficient lexicographic comparison is the hard part. Storing full paths at a scale of 2 * 10^5 nodes can be expensive. In practice, parent pointers with on-demand comparison, or processing in time order using the strictly increasing arrival times, together with path ranking or persistent comparison, can help. An evaluation need not demand the most optimized engineering implementation, but it should check whether the model realizes that path order cannot be settled by a simple reverse comparison at the end, and whether equivalent paths are handled.

    3.3 Common failure patterns

    The tested model did worst on this problem. Typical mistakes include ignoring the wait until the next legal departure; using floating-point modulo and losing precision on large integers; optimizing only arrival time and ignoring cost; ignoring lexicographic order after costs match; returning an empty path instead of a single-node path when s == t; keeping only one of several multi-edges; returning an empty list instead of None when the target is unreachable; and missing a complexity analysis while still comparing full path strings.

    3.4 Evaluation checkpoints

    Cover at least: the degenerate case period = 1; many equivalent paths when cost = 0; equal earliest times with different costs; equal arrival and cost with different path order; start equal to target; multi-edges that differ in cost or time; a very large start_time; an unreachable target; and waiting on a cycle edge. Each case should check all three components of the returned triple, not only the arrival time.

    4. TC2: Idempotent replay of out-of-order logs and snapshots

    4.1 Key difficulties

    The core is not sorting; it is state-machine semantics. Logs must first be replayed in ascending order of (ts, actor, op_id). Under the same actor, the same op_id may take effect only once, and later duplicates are ignored. The same op_id under different actors does not interfere. add creates an object only if it does not exist and is ignored otherwise. move takes effect only when the current owner equals the actor. delete also requires an ownership check. snapshot records the visible state of the given ids at that moment, stores None for a missing id, and must not change object state.

    A detail that is easy to miss is when the idempotency mark is written. The usual rule is to check and mark (actor, op_id) before handling the log, so later duplicates are ignored. Even if the first attempt changes no state because of insufficient permission, that idempotency key should still be treated as consumed. Otherwise a repeated log may be processed again, violating the “takes effect only once” rule.

    4.2 Implementation structure

    Object state can live in a dictionary, consumed idempotency keys in a set, and snapshots in a list or dictionary. Pseudocode:

    1. objects = {}
    2. seen = set()
    3. snapshots = []
    4. for event in sorted(events, key=(ts, actor, op_id)):
    5. key = (actor, op_id)
    6. if key in seen:
    7. continue
    8. seen.add(key)
    9. if kind == "add":
    10. id, value = payload
    11. if id not in objects:
    12. objects[id] = {owner: actor, value: value}
    13. elif kind == "move":
    14. id, new_owner = payload
    15. if id in objects and objects[id].owner == actor:
    16. objects[id].owner = new_owner
    17. elif kind == "delete":
    18. id = payload
    19. if id in objects and objects[id].owner == actor:
    20. del objects[id]
    21. elif kind == "snapshot":
    22. label, ids = payload
    23. record = []
    24. for id in ids:
    25. if id in objects:
    26. record.append((id, objects[id]))
    27. else:
    28. record.append((id, None))
    29. snapshots.append((label, record))

    A real implementation must also fix whether a snapshot stores the value, the owner, or the full object; whether ids may contain duplicates; and the order and structure of snapshot output. Those ambiguities should be fixed in the problem statement.

    4.3 Common failure patterns

    The tested model performed only moderately here. Typical mistakes include dropping actor or op_id from the sort key; using a global op_id instead of (actor, op_id) in the idempotency set; marking the key only after processing; forgetting the ownership check on move and delete; letting snapshot modify objects or delete missing ones; skipping a missing id in a snapshot instead of recording None; ignoring sort stability at the same timestamp; and handling repeated op_ids inconsistently across operation types.

    4.4 Evaluation checkpoints

    Cover repeated op_ids for the same actor; the same op_id for different actors; repeated add; move by a non-owner; delete by a non-owner; object state unchanged before and after a snapshot; a snapshot that includes a missing id; sorting of out-of-order logs; the same timestamp with different actors; an empty log; and duplicate snapshot labels. Differential testing can generate random logs and a naive replayer, then compare the final object state and the snapshot sequence.

    5. From wrong answers to a capability profile

    The results can be summarized as a capability profile. The tested model is usually not weak at local code generation, but it loses points on keeping a long specification, multi-objective priority, hidden boundary coverage, state-machine invariants, and self-verification. The failures fall into four groups:

    1. Missing specification details: constraints in the statement are not applied one by one, such as lexicographic order or the scope of idempotency.
    2. Missing boundaries: degenerate cases are not covered, such as period = 1, s == t, an empty log, or a missing object.
    3. Complexity mismatch: the implementation passes small samples but cannot meet large-scale constraints.
    4. Insufficient verification: the model does not build differential or property tests, so errors stay hidden until review.

    6. Practical recommendations

    For a team, evaluating an LLM should not stop at the final code. It should cover the full chain of specification understanding, design, implementation, and verification. Prompts should ask the model to list invariants, boundary conditions, and complexity targets before the implementation. Review should use hidden cases, random differential tests, and property tests. Multi-objective problems should state priority explicitly, and state-machine problems should state the idempotency-key scope and the read-only nature of snapshots. Only when evaluation is reproducible, explainable, and extensible does model comparison become meaningful, and only then can it actually guide selection and use in real development.