Data Structures & AlgorithmsHardTCS NQT · Infosys pattern
What is the postfix (Reverse Polish) form of the infix expression "(c + i)* a - j / d", built with the standard stack-based conversion (^ right-associative, others left-associative)?
- A. c i + a * j - d /
- B. c i + a * j d / -
- C. - / d j * a + i c
- D. c i a j d / - * +
Correct answer: B. c i + a * j d / -
Using a stack to hold pending operators and popping higher-or-equal precedence operators before pushing a new one (left-associative) gives "c i + a * j d / -". Treating all operators as equal precedence instead would wrongly give "c i + a * j - d /".
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [1, 0, 1, 2, 2, 2, 1, 1] (values 0, 1, 2 only) in…
- Elevation map [6, 1, 1, 0, 3, 3, 5, 5, 2, 0] (bar width 1). Using the two-pointer / prefix-max approach for trapping rain water, how much…
- Array [75, 79, 80, 82, 6, 11, 23, 40, 63, 67] is a sorted array rotated at pivot 6. Using the standard O(log n) rotated-array binary…
- Array [1, 2, 3, 4, 5, 6, 7, 8] is left-rotated by d = 7 positions using the reversal algorithm (reverse first d, reverse rest, reverse…
- What is the sum of the minimum element over every contiguous subarray of [7, 6, 6, 4, 8, 9]?
- Using a sliding window of fixed size with a character-frequency count, how many substrings of "BABAACBCCA" are anagrams of "AB"?