Skip to question
SystemVerilogDesignVerificationFirmwareArchitectureASIC Interview Questions→
/Interview questions/Add decimal lists from a fixed pool

Q083·Free·Firmware

Add decimal lists from a fixed pool

Difficulty
Medium
Topic
Firmware Algorithms
Language
C
Interview prompt

Question

Add two least-significant-digit-first decimal lists from a bounded SRAM node pool into a separate fixed destination pool without partial output. Use the supplied span helpers on a flat-address C platform where uintptr_t preserves byte addresses. Nonnull arguments identify live, correctly aligned objects of their declared types and capacities. UINT8_MAX marks an empty list; two empty lists sum to the single zero node. The complete destination span and both metadata objects must be mutually disjoint and disjoint from the active source pool. A zero-capacity destination may be null. Validate arguments and source lists before deciding whether capacity is sufficient. The source pool must be nonnull and src_count must be 1 through 32 even when both heads are UINT8_MAX.

Candidate starting point

Implementation scaffold

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

typedef struct { uint8_t digit; uint8_t next; } digit_node_t;
typedef enum { ADD_OK, ADD_BAD_ARG, ADD_BAD_LIST, ADD_NO_SPACE } add_status_t;

static bool add_span(const void *pointer, size_t bytes,
                     uintptr_t *first, uintptr_t *last) {
  const uintptr_t begin = (uintptr_t)pointer;
  if (bytes > UINTPTR_MAX - begin) return false;
  *first = begin;
  *last = begin + bytes;
  return true;
}

static bool add_overlap(uintptr_t a_first, uintptr_t a_last,
                        uintptr_t b_first, uintptr_t b_last) {
  return a_first < b_last && b_first < a_last;
}

static bool validate_digit_list(const digit_node_t *src, size_t src_count,
                                uint8_t head, uint32_t *visited) {
  // TODO: validate one list and return its visited-node mask.
}

add_status_t add_decimal_lists(const digit_node_t *src, size_t src_count,
    uint8_t a_head, uint8_t b_head, digit_node_t *dst, size_t dst_cap,
    uint8_t *dst_head, size_t *dst_used) {
  // TODO: validate spans and both walks, stage the carry pass, then commit.
}
Reviewed example

Trace one case

Input
LSF decimal list A=[9,9] (99); B=[1]
Expected output
destination list=[0,0,1] (100)

The carry propagates through both existing digits and allocates one final standard (canonical) digit after the sizing pass confirms capacity.

What to cover

Requirements

  1. Reject bad pointers, overlap, bad digits or links, cycles, and any source node shared by both lists.
  2. Use no heap or recursion and at most two 32-bit visited masks for source validation.
  3. Run a sizing carry pass before writing and return no-space without changing the destination or output metadata.
  4. Write a canonical consecutive result, propagating final carry and representing zero with one zero node.
Exact question handoffPractice Q083

Solve it in the question bank, keep your progress, and reveal the reviewed solution when your access allows.

Open in question bank →
Solution accessEach time you open this Solution, one Practice Credit is used; it is not permanently unlocked. Premium Solution content also uses one credit per opening.
Continue learning

Firmware Guide

Review algorithms, data structures, fixed-memory reasoning, concurrency, and silicon bring-up.

  • Firmware Algorithms
  • Linked lists
  • Fixed memory
  • Validation
Firmware Guide →
Continue practicing

Related questions

Q064 · Firmware AlgorithmsEnumerate calibration sums with fixed memoryMedium→Q089 · Firmware AlgorithmsParse a bounded Roman board revisionMedium→
ASIC.FYI · Learn silicon end to end.info@asic.fyi