Skip to question
SystemVerilogDesignVerificationFirmwareArchitectureASIC Interview Questions→
/Interview questions/Intersect redundant-core fault lists

Q027·Free·Firmware

Intersect redundant-core fault lists

Difficulty
Easy
Topic
Firmware Algorithms
Language
C
Interview prompt

Question

Return the multiset intersection of two bounded fault-ID lists, preserving the first list's encounter order and consuming each match only once. Use C11 on the stated flat-address platform, where uintptr_t preserves byte addresses. Nonempty spans refer to live objects of the stated type and length. Null is allowed only for a zero-length input or zero-capacity output buffer; nout always identifies a writable size_t object. Validate arithmetic and alignment, then require the entire declared output buffer and nout to be disjoint from each other and from both active inputs. The two read-only inputs may overlap. Descriptor errors take priority over insufficient capacity.

Candidate starting point

Implementation scaffold

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

typedef enum { IX_OK, IX_EINVAL, IX_ENOSPC } ix_rc_t;
enum { IX_MAX = 32 };

// Supplied flat-address span helpers. Zero bytes require no object.
static bool valid_span(const void *ptr, size_t bytes, size_t alignment) {
  if (bytes == 0) return true;
  const uintptr_t base = (uintptr_t)ptr;
  return ptr != NULL && base % alignment == 0 && bytes <= UINTPTR_MAX - base;
}
// Call only after validating both nonempty spans.
static bool overlap(const void *a, size_t an, const void *b, size_t bn) {
  if (an == 0 || bn == 0) return false;
  const uintptr_t ab = (uintptr_t)a, bb = (uintptr_t)b;
  return ab < bb + bn && bb < ab + an;
}

ix_rc_t fault_intersection(const uint16_t *a, size_t na,
                           const uint16_t *b, size_t nb,
                           uint16_t *out, size_t cap, size_t *nout) {
  // TODO: validate all spans, stage stable matches, then commit both outputs.
}
Reviewed example

Trace one case

Input
A=[5,2,5,7]; B=[5,5,8]
Expected output
intersection=[5,5]

Both copies of 5 are consumed once in A encounter order; 2 and 7 have no available match.

What to cover

Requirements

  1. Emit each identifier min(count_a, count_b) times in A's encounter order.
  2. Use fixed state for at most 32 entries and do not modify either input.
  3. Count the complete result before writing and return no-space without exposing a partial output.
  4. Validate lengths, pointers, byte-count/address arithmetic and alignment before reading inputs. Reject any overlap between the declared output buffer, nout and the active inputs; the inputs may overlap each other. Leave both outputs and both inputs unchanged on every error.
Exact question handoffPractice Q027

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
  • Arrays
  • Multiset
  • Capacity
Firmware Guide →
Continue practicing

Related questions

Q083 · Firmware AlgorithmsAdd decimal lists from a fixed poolMedium→Q064 · Firmware AlgorithmsEnumerate calibration sums with fixed memoryMedium→
ASIC.FYI · Learn silicon end to end.info@asic.fyi