3AC, Quadruples and Triples Interview Questions: Worked Answers

Translate one expression into 3AC, quadruples and triples, then explain which instructions can move. Worked answers show why indirect triples preserve references and why dependencies still matter.

KnowledgeGate Team

Exam prep & CS education

Updated 11 Nov 20265 min read
3AC, Quadruples and Triples Interview Questions: Worked Answers

Recalling four field names is easy; interviewers care whether you can explain what each field stores and when a reference becomes unsafe. Connect each field to the value it represents and the instruction that produces it. The earlier Three-Address Code (3AC): Quadruples, Triples and Indirect Triples with a Worked Example guide develops the full record-level comparison; the interview prompts here practise concise answers and the dependency checks behind safe triple reordering.

What is three-address code, and does every instruction use three addresses?

Prompt: Why does t1 = a + b count as three-address code?

Model answer: It has two source operands, a and b, and one destination, t1. The operator + is not an address. Here, addresses can be names, constants or compiler-generated temporaries; they need not be literal physical memory addresses.

These basic assignment forms use at most three addresses:

  • t1 = a + b: binary operation, three addresses.

  • t2 = -c: unary operation, two addresses.

  • x = t2: copy, two addresses.

Quadruples and triples are record representations of such instructions, not additional compiler phases or different translation algorithms. For the broader compiler-design path, read intermediate code generation for unary-minus lowering, control flow and DAG optimisation. The record-level interview distinction is narrower: named quadruple results, positional triple references and pointer-controlled execution order.

How do you translate an expression into 3AC?

Prompt: Translate x = (a + b) * (c - d) and explain its dependencies.

Assume ordinary integer arithmetic with no side effects, overflow or exceptions. The symbolic form is:

Code
t1 = a + b
t2 = c - d
t3 = t1 * t2
x = t3

For a runtime check, take a = 6, b = 2, c = 9 and d = 4. First, t1 = 6 + 2 = 8. Next, t2 = 9 - 4 = 5. Therefore, t3 = 8 * 5 = 40, and the copy gives x = 40.

The first two instructions are independent. Multiplication needs both results, and the final copy needs the multiplication result.

Follow-up: Could this use fewer instructions? Yes. Replace the final two lines with x = t1 * t2. The four-instruction form keeps the quadruple and triple tables row-for-row comparable.

Flow diagram for x = (a + b) * (c - d): independent t1 = 8 and t2 = 5 feed t3 = 40, which feeds x = 40.

What are the quadruples for that expression?

Prompt: Write the quadruples and identify the result field.

Each record has four fields: (op, arg1, arg2, result). Indices below start at zero; unused denotes an empty field.

Index

op

arg1

arg2

result

0

+

a

b

t1

1

-

c

d

t2

2

*

t1

t2

t3

3

=

t3

unused

x

Model answer: The copy still has a destination, x; only its second argument is unused. The index is a separate display label.

If the independent first two rows swap, their results remain named t1 and t2. Multiplication therefore keeps the same operand names. This preserves these local data references.

How do triples remove the explicit result field?

Prompt: Write the same sequence as triples.

Before filling the table, define the notation: (i) refers to the result produced by record i, not the literal integer i. Our copy convention is (=, source, destination). Other textbooks may reverse copy operands, so state your convention explicitly.

Index

op

arg1

arg2

0

+

a

b

1

-

c

d

2

*

(0)

(1)

3

=

(2)

x

Model answer: Records 0, 1 and 2 implicitly produce 8, 5 and 40. Record 3 stores record 2's value into x. Its arg2 is a destination because this is a copy; that rule does not apply to every operation.

The row number labels the producer without adding a fourth stored result field. Triples save one stored field per record.

Why do indirect triples help when instructions move?

Prompt: Swap the independent addition and subtraction. What must be repaired?

If ordinary triples are physically reordered and renumbered, the records become:

Code
0: (-, c, d)
1: (+, a, b)
2: (*, (1), (0))
3: (=, (2), x)

Record 2's references change to preserve producer identities. Multiplication happens to commute, but coincidentally equal outputs cannot justify leaving references unrepaired.

Indirect triples retain the original records and use an execution-pointer list. Change [0, 1, 2, 3] to [1, 0, 2, 3]; the stored multiplication remains (*, (0), (1)). Its references identify stable records, not positions in the execution list.

NYU's compiler lecture notes on quadruples and indirect triples, sections 6.2.2–6.2.3, describe these record layouts and explain why moving pointers to fixed triples avoids repairing their producer references.

Indirection adds a pointer structure. Reject [2, 0, 1, 3]: multiplication would run before either operand was produced.

Indirect triples diagram with fixed records 0 through 3 and execution order changing from [0, 1, 2, 3] to [1, 0, 2, 3]; producer references stay stable and x = 40.

Which interview traps reveal shallow understanding?

Unary minus: Confusing unary and binary operators leads to inventing a second input. For t1 = -c, use (uminus, c, unused, t1). With c = 9, the result is -9.

Repeated expressions: Treating translation as automatic optimisation can silently delete an evaluation. For y = (a + b) * (a + b), an unoptimised translation is:

Code
u1 = a + b
u2 = a + b
u3 = u1 * u2
y = u3

With unchanged operands, reuse is valid:

Code
u1 = a + b
u2 = u1 * u1
y = u2

For a = 6, b = 2, the first sequence computes 8 * 8 = 64; the second computes u1 = 8, then u2 = 8 * 8 = 64. Both store 64 in y.

Performance: Fewer intermediate records do not guarantee a runtime speedup.

How do you practise the interview and written-test versions?

Use these rehearsal prompts: “Explain quadruples in 30 seconds”, “Convert the expression”, “Defend the safe swap” and “Explain why indirect triples still need dependency checks”. For written answers, state indexing and copy conventions first.

Now use subtraction: z = (a + b) - (c - d). The same inputs give 8 - 5 = 3. Initial triples are:

Code
0: (+, a, b)
1: (-, c, d)
2: (-, (0), (1))
3: (=, (2), z)

After physically swapping rows 0 and 1, repair row 2 to (-, (1), (0)). It still computes 8 - 5 = 3. Unchanged references instead compute 5 - 8 = -3, exposing the mistake multiplication hid. Continue with three-address-code worked practice once you can explain that sign change.

What should you remember before your next interview?

3AC splits operations into simple instructions. Quadruples name results; triples refer to result-producing records. Indirect triples separate record identity from execution order.

Spend five minutes reconstructing both tables without notes, then explain the valid swap aloud. Change multiplication to subtraction and account for both 3 and the incorrect -3. If you can identify the producer behind every operand, you understand more than the field names.

For structured study of the foundations, explore the Compiler Design course.