InterviewLegend

Find the Bug & Predict the Output in C and C++

Alex Chen40 min readGuide updated 61 real interview reports
In this lesson
  • Fix a linked-list insertAfter with a missing pointer, and predict virtual and non-virtual calls
  • Copy overlapping memory correctly and explain why memcpy can't
  • Predict pointer, static and signed-unsigned output from the C rule behind each one
  • Debug a bit-pair swap and extend it to a full 32-bit reversal

AMD has one coding habit that shows up more reliably than any LeetCode pattern. An interviewer puts a short C or C++ program in front of you and asks what's wrong with it, or what it prints, and you answer without running it. Candidates have described this since 2019 in more than a dozen first-hand accounts, from intern rounds in Cambridge and Santa Clara to full-time interviews in Bengaluru and Markham. Two of them are from August 2026.

The code is short and often a little strange on purpose. A co-op candidate in 2024 described it as "very oddly written C code" and was asked how it would run and what it would print. The bugs come from a small family. There's a pointer that never gets linked, a copy that tramples its own source, an int compared with an unsigned int and a mask shifted the wrong way.

Each problem below starts from the round a candidate described, shows a snippet something like the one they saw, then gives you a runnable version. Where the round was "find the bug", the starter code carries the bug and you fix it. Where it was "predict the output", you write a small function that computes what C would do, which forces you to know the rule instead of half-remembering it.

The AMD angle
Say the rule out loud, then the answer. The August 2026 output round wanted the why along with the prediction, then pushed on it with follow-ups. "It prints 4294967295" is weaker than "the int converts to unsigned before the compare, so -1 becomes the largest value". Name the C rule first and the output follows from it.

How to read code you can't run

Read the code twice. The first pass is for shape: what each function promises, what it takes and what it hands back. The second pass is a trace with one small input, written as a table of variables with a row for every line that changes something. Most planted bugs show up on that second pass as a row that disagrees with the promise from the first.

Before you trace, scan for the usual C suspects. Pointer assignments that don't match the picture you'd draw are one. So are comparisons that mix signed and unsigned types, copies whose source and destination overlap, shifts aimed at the wrong bits and statics that survive between calls. When the task is to make a file compile, read the includes and the declarations before the logic.

Draw the boxes and arrows. For pointer code, a sketch of each node and where each pointer points catches more bugs than rereading. In a linked-list insert, the arrow you can't find a line of code for is the bug.

Fix a linked-list insertAfter, then predict the overriding output

In August 2026 a candidate for a software engineer role on EDA tools had three rounds in one day, and the third was all output and debugging questions. They remembered two of the three. One was a linked-list function that adds a value after the first node holding another value, with a pointer missing. The other was code built on method overriding, where the question was what it prints and why, followed by follow-ups.

You'll fix the insert in runnable form below. In C++ the broken version looks something like this:

cpp
void insertAfter(Node* head, int afterVal, int val) {
Node* cur = head;
while (cur != nullptr && cur->value != afterVal)
cur = cur->next;
if (cur == nullptr)
return; // afterVal is not in the list
Node* node = new Node(val);
node->next = cur->next;
}

The overriding question is pure prediction. Something like this, asked as "what does it print, and why?":

cpp
struct Shape {
void name() { cout << "Shape "; }
virtual void draw() { cout << "Shape "; }
};
struct Circle : Shape {
void name() { cout << "Circle "; }
void draw() override { cout << "Circle "; }
};
Shape* s = new Circle();
s->name();
s->draw();

It prints Shape Circle. The call to name is fixed at compile time from the pointer's declared type, so Shape's version runs. draw is virtual, so the call goes through the object's vtable at run time and lands in Circle's. Through a Circle object directly, both calls print Circle.

Rehearse the edges of that rule, since follow-ups live there. A virtual call made inside Shape's constructor runs Shape's version, because the Circle part doesn't exist yet. Deleting a Circle through a Shape* without a virtual destructor is undefined behavior. And override makes the compiler reject a draw whose signature drifts from the base.

1

Linked List Insert After a Value

EasyFrequently asked
Problem
The starter code builds a singly linked list from values. Its method insert_after(after_val, val) should find the first node holding after_val, put a new node holding val right after it, and return true. If no node holds after_val, the list stays as it is and the method returns false.

The starter has a bug. insert_after returns true, but the new value never appears in the list. Find it and fix it. to_list() returns the values from head to tail.
  1. LinkedList([1, 2, 3])
  2. insert_after(2, 9)→ true
  3. to_list()→ [1,2,9,3]

The new node holding 9 goes right after the node holding 2.

How this uses the pattern

An insert is two pointer writes. The new node takes the found node's old next, then the found node points at the new node. Do them the other way round and you lose the rest of the list.

Write it here, or go full screen with the problem, hints and solution beside the editor.

Linked List Insert After a Value

Loading...
Click "Run" to execute your code
The AMD angle
In 2026 this came in C++ with one line missing, and the job was to find it by reading. Draw the three nodes and both arrows the insert should leave behind, then match each arrow to the line that writes it. One arrow, from the new node into the list, gets written. Nothing ever points at the new node, so it hangs off the side. In C++ that's also a leak, since the node is unreachable once the function returns, and saying so counts, because this round wanted reasons along with fixes.
Take it further
Insert before a value, and delete a value. Both need the node before the target. Walk with a prev pointer, or with a pointer to the link itself (Node** link = &head) and rewrite *link. The pointer-to-pointer version handles the head with no special case, which is the cleaner answer if the interviewer asks what happens when the target is the first node.

Find what's wrong in a C function

The other August 2026 account is from a software engineer interview in Markham. After the behavioral questions came a coding assessment: debug the problem in the given code. The candidate called it very open ended, with room for conversation and hints, and was also asked how polymorphism works in their chosen language. A UK software developer round in 2025 ran close to it. That candidate debugged a C snippet without executing it, then got questions on static variables and memcpy.

Neither candidate shared the code. The two topics the 2025 round named each hide a classic bug, so they're the right place to practice. Static locals come first, as a quick prediction:

c
int next_id(void) {
static int count = 0;
count++;
return count;
}
int main(void) {
printf("%d ", next_id());
printf("%d ", next_id());
printf("%d\n", next_id());
}

It prints 1 2 3. A static local is initialized once, before the program starts, and keeps its value between calls. Drop static and it prints 1 1 1. Have two follow-ups ready. Put all three calls in one printf and C leaves the order they run in unspecified, so any order is legal. And a function with a static counter isn't safe to call from two threads at once.

memcpy is the runnable problem. Its contract says the source and destination must not overlap, and a copy that walks forward breaks the moment they do. Something like this:

c
void make_room(char *buf, size_t n) {
// shift the first n bytes one place right
memcpy(buf + 1, buf, n);
}

The two ranges overlap, so the behavior is undefined. A plain forward byte loop, the simplest way to write memcpy, overwrites buf[1] before reading it and smears the first byte across the whole range. memmove exists for exactly this case.

2

Copy Overlapping Memory Like memmove

EasySometimes asked
Problem
The starter code should behave like C's memmove. Given a buffer buf of byte values, it copies the n values that start at index src into the n positions that start at index dest, then returns the buffer. Both ranges always fit inside the buffer.

The ranges may overlap. The starter copies front to back, which gives the wrong answer for some overlaps. Find the cases it gets wrong and fix it, so the result always matches copying the original n values.
Inputbuf = [1, 2, 3, 4, 5, 6]dest = 2src = 0n = 3Output[1, 2, 1, 2, 3, 6]

The values 1, 2, 3 land at indices 2 to 4. The front-to-back starter overwrites index 2 before reading it and returns [1, 2, 1, 2, 1, 6].

How this uses the pattern

When the destination starts after the source, copy from the back. Otherwise front to back is safe. A plain copy plus that one branch gives you memmove.

Write it here, or go full screen with the problem, hints and solution beside the editor.

Copy Overlapping Memory Like memmove

Loading...
Click "Run" to execute your code
The AMD angle
The 2025 round named memcpy alongside static variables, and overlap is the bug memcpy is famous for. Read any copy loop with one question in mind: does a write land on a value the loop hasn't read yet? Moving data toward higher addresses with a forward loop does exactly that. The fix is to copy backward when dest > src, or to call memmove, which picks the direction for you. Say both, and say why memcpy skips the check. Free to assume no overlap, it can copy in any order or in wide chunks, which is where its speed comes from.
Take it further
Write memmove for raw bytes in C. Take void *dest, const void *src, size_t n, cast both to unsigned char *, and compare the pointers to pick a direction. Then the question becomes how a real implementation goes faster. It copies a few bytes to reach an aligned address, moves whole words at a time, and finishes the tail byte by byte.

Older problems (2021-2025)

The next three each come from a single account between 2021 and 2025, so treat them as practice for the shape rather than a forecast. The C rules underneath them are the ones a find-the-bug snippet turns on most often: how arguments pass, how types convert and how bits move.

Fix a file so it compiles, then predict pointer calls

A Bengaluru software engineer panel in February 2025 had two tasks of this kind: fix a file that didn't compile, and work out the output of several function calls, some of them through pointers. The compile half is mostly bookkeeping. Look for a missing include behind malloc or printf, a function called before it's declared, and a pointer stored in a plain int. The output half comes down to two rules, and a snippet something like this tests the first:

c
void reset(int *p) { p = NULL; }
void bump(int *p) { (*p)++; }
void swap(int a, int b) { int t = a; a = b; b = t; }
int main(void) {
int x = 1, y = 5;
int *p = &x;
reset(p);
bump(p);
swap(x, y);
printf("%d %d %d\n", x, y, *p);
}

It prints 2 5 2. C passes every argument by value, pointers included. reset sets its own copy of p to NULL. The caller's p still points at x. bump changes x through the pointer, so that change sticks. swap trades two copies and leaves x and y alone. The second rule is precedence, and you'll code it up below. *p++ reads the value and then moves the pointer, while (*p)++ bumps the value.

3

Trace Pointer Increments Through an Array

EasySometimes asked
Problem
An int *p starts at index 0 of the array values. You get a list of C expressions in ops, run in order, each one of "*p", "*p++", "*++p", "++*p" or "(*p)++". Return the value each expression evaluates to, in order.

An expression that moves the pointer moves it for every expression after it. An expression that changes a value writes it back into the array. The pointer never reads outside the array.
Inputvalues = [10, 20, 30]ops = ["*p++", "*p", "(*p)++", "*p", "++*p", "*++p"]Output[10, 20, 20, 21, 22, 30]

*p++ reads 10 and moves to index 1. (*p)++ returns 20 and stores 21, ++*p raises it to 22, and *++p moves on and reads 30.

How this uses the pattern

Postfix ++ binds tighter than *, while prefix ++ and * bind right to left. Parentheses override both. Read each expression from the operator nearest p outward.

Write it here, or go full screen with the problem, hints and solution beside the editor.

Trace Pointer Increments Through an Array

Loading...
Click "Run" to execute your code
The AMD angle
This is the predict-the-output half of the 2025 panel, turned into code you can check. Writing the emulator forces each rule into one line, which is how you want to hold it when something like while (*dst++ = *src++); lands in front of you. For each expression, say what it returns and what it changes, as two separate facts. Most wrong answers mix the two up, as with (*p)++, which returns the old value while the array already holds the new one.
Take it further
Add p[1] and a second pointer. p[i] means *(p + i), so it reads without moving anything. With two pointers into one array, a write through either is visible through both, which is where aliasing questions start. Keep the array as the single source of truth and store each pointer as an index.

Predict a signed and unsigned comparison

In April 2023 an intern candidate in Cambridge got two coding questions from two interviewers in the technical stage. One was C code that compared a negative signed integer with an unsigned integer, and the question was what it would print. Something like:

c
int a = -1;
unsigned int b = 1;
if (a < b)
printf("less\n");
else
printf("not less\n");

It prints not less. When an int meets an unsigned int in a comparison, the usual arithmetic conversions turn the int into an unsigned int. With a 32-bit int, -1 becomes 4294967295, the largest value the type holds, so a < b is false. Mention that -Wsign-compare catches it.

4

Signed and Unsigned Comparison in C

EasySometimes asked
Problem
Write c_less_than(a, a_unsigned, b, b_unsigned), which returns what the C expression a < b evaluates to, as a boolean. Each operand is a 32-bit int when its flag is false and an unsigned int when its flag is true.

The values are what the source assigns. An unsigned operand assigned a negative number wraps modulo 2^32 first, the way unsigned int x = -5; does. Signed values always fit in 32 bits.
Inputa = -1a_unsigned = falseb = 1b_unsigned = trueOutputfalse

a converts to unsigned and becomes 4294967295, which is not less than 1.

How this uses the pattern

Normalize each value to its own type first. Then apply the rule: if either side is unsigned int, both compare as unsigned int, and otherwise as signed.

Write it here, or go full screen with the problem, hints and solution beside the editor.

Signed and Unsigned Comparison in C

Loading...
Click "Run" to execute your code
The AMD angle
This has a short answer and a long why, and the why is what gets scored. The rule has a name, the usual arithmetic conversions, and the part that matters fits in a sentence: when int meets unsigned int, the int converts. Then say what that does to -1. In two's complement the bit pattern stays the same and only its meaning changes. The same rule explains why for (unsigned i = n - 1; i >= 0; i--) never ends, a bug that turns up in find-the-bug code too.
Take it further
Narrower and wider types. An unsigned short or unsigned char promotes to int before the compare, so -1 < (unsigned short)1 is true. sizeof returns size_t, an unsigned type at least as wide as unsigned int, so -1 < sizeof(int) is false. To model it, add a bit width per operand and promote anything narrower than int to signed int before applying the rule.

Debug a program that swaps adjacent bits

A software engineer interview in Singapore in June 2021 had about 50 lines of C to debug over screen share, which the candidate called pretty easy. The program swaps the first two bits with each other, the next two with each other, and so on up to 32 bits, so 1100 0110 becomes 1100 1001. Its core looks something like this:

c
uint32_t swap_pairs(uint32_t x) {
uint32_t high = x & 0xAAAAAAAA; // upper bit of each pair
uint32_t low = x & 0x55555555; // lower bit of each pair
return (high << 1) | (low >> 1);
}

Trace it on the example before you study the masks. The low byte 1100 0110 should come out as 1100 1001, and this version gets something else. That gap points you at the shifts.

5

Swap Each Pair of Adjacent Bits

EasySometimes asked
Problem
Given a 32-bit unsigned value n, swap bit 0 with bit 1, bit 2 with bit 3, and so on up to bits 30 and 31. Return the result as an unsigned value.

The starter code masks the two halves of each pair correctly but returns the wrong value. Find the bug and fix it. For example, 1100 0110 (198) becomes 1100 1001 (201).
Inputn = 198Output201

In binary 1100 0110 becomes 1100 1001. Read as pairs, 11 00 01 10 turns into 11 00 10 01.

How this uses the pattern

One mask picks the upper bit of every pair and the other picks the lower bit. Move the upper bits down one place, the lower bits up one place, and OR the two together.

Write it here, or go full screen with the problem, hints and solution beside the editor.

Swap Each Pair of Adjacent Bits

Loading...
Click "Run" to execute your code
The AMD angle
The 2021 program ran about 50 lines, which is long for a two-line idea. In a listing that size the bug sits in the arithmetic, so find the line that does the real work and run the prompt's own example through it. Then say what the masks mean: 0xAAAAAAAA holds bits 1, 3, 5 and up, the upper bit of each pair, and 0x55555555 holds the lower ones. In C, also check the type of x. Shifting a 1 into the sign bit of a signed int is undefined behavior, so the value belongs in a uint32_t.
Take it further
Reverse all 32 bits. Swapping pairs is the first step of the classic branch-free bit reversal. Repeat the same move on 2-bit groups, nibbles and bytes with the masks 0xCCCCCCCC, 0xF0F0F0F0 and 0xFF00FF00 and their complements, then swap the two 16-bit halves. That reverses the whole word in five steps.

Two more shapes from those years get a line each. An intern candidate in Austin in July 2025 got a behavioral interview and then a round debugging some assembly code, so for firmware or driver roles be ready to step through a short listing register by register. A secondhand 2021 account from an MTech internship in India had a C snippet that stored malloc(n * sizeof(int)) in a plain int, dropped the semicolon on that line and included only stdio.h. The fixes are an int *, the semicolon and #include .

Common questions

What C and C++ questions come up in an AMD technical interview?

Next to a LeetCode-style problem or two, expect code you read by hand. You find the bug, fix the compile error or say what it prints. The topics in these rounds are pointers and linked lists, overriding and virtual calls, static variables, memcpy, signed and unsigned types and bit masks. Theory questions on C++, operating systems and computer architecture sit beside them, and the next lesson covers those.

Can I run the code during the round?

Plan as if you can't. The 2025 UK round said so outright, a C snippet debugged "without executing it", and the August 2026 rounds were built around predicting output and talking through a fix. Trace on paper or in a plain editor, and narrate the trace as you go so the interviewer can follow your reasoning and drop a hint.

How should I practice for this round?

Work the five problems here from the buggy starters and say the bug before you change a line. Then take a C file you didn't write, a few hundred lines from any open-source project, and predict what each function returns on a small input before you check. A short session of that most days builds the habit this round tests.

Key Takeaways

  • ✓AMD keeps asking candidates to read C or C++ without running it: two rounds in August 2026, about a dozen more back to 2019.
  • ✓Draw the pointers for linked-list bugs. The missing insertAfter line is the one linking the found node to the new node.
  • ✓Name the C rule before the output: pass by value, operator precedence, signed meets unsigned, virtual dispatch.
  • ✓memcpy assumes no overlap. Copy backward when the destination starts after the source, which is what memmove does.
AMD interview guide

Know what AMD asks before you walk in.

37 coding · 5 system design · 17 behavioral questions

Get Premium$49 a year · Instant access
Or start freeNo card.

Get their questions first.

What AMD Tests in CodingHad your AMD interview?How we research

InterviewLegend is independent and not affiliated with or endorsed by any company it covers.