GATE Computer ScienceHardGATE CS (PYQ pattern)

A DAG is built for r = (p − q) * (p − q) + (q − p) * (p − q) − (p − q), with * commutative and − not commutative. How many interior (operator) nodes does it have?

  1. A. 6
  2. B. 4
  3. C. 5
  4. D. 9

Correct answer: A. 6

Interior nodes: p − q (all three occurrences share it), q − p (a different node, since − is not commutative), (p−q)*(p−q), (q−p)*(p−q), the + and the final −: 6. Wrongly merging q − p with p − q would also merge the two products, giving 4; the expression tree has 9 operators.

More Compiler Design questions

Exams that ask this topic