Data Structures & AlgorithmsVery hardProduct company OA

A stack-based decoder expands k[s] into s repeated k times (nesting allowed). What does '2[x3[y]z]q' decode to?

  1. A. xyyyzqxyyyzq
  2. B. xyyyzxyyyzq
  3. C. xyzxyzxyzq
  4. D. xyyyzq

Correct answer: B. xyyyzxyyyzq

Innermost first: 3[y] = yyy, so the bracket is xyyyz; 2[…] doubles it to xyyyzxyyyz; q lies outside every bracket and is appended once. Applying 2 to the trailing q as well gives xyyyzqxyyyzq; repeating 'xyz' three times gives xyzxyzxyzq.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic