Data Structures & AlgorithmsModerateCognizant · Capgemini pattern
What is the value of the prefix expression − * + 2 3 4 / 8 2 ?
- A. 16
- B. 4
- C. 18
- D. -16
Correct answer: A. 16
Reading it as a tree: −( *( +(2, 3), 4 ), /(8, 2) ) = (5 × 4) − 4 = 16. Scanning right to left with a stack, the first popped value is the LEFT operand; swapping them gives 4 − 20 = −16.
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
- Infix A (B + C (D − E)) / F is converted to postfix with an operator stack (operators and '(' are pushed onto it). What is the largest…
- Using a sliding window, find the length of the longest substring with no repeated character in the string dvdfabcda.
- What is the length of the shortest contiguous subarray of [1, 4, 4, 2, 3, 1, 5] whose sum is at least 9?
- How many contiguous subarrays of [1, 2, 3, −3, 3, 1] have sum exactly 3? (A prefix-sum + hash-map method counts them in O(n).)
- How many UNIQUE triplets (as sets of values, ignoring order) of [−1, 0, 1, 2, −1, −4, −2, 3] sum to 0? (Sort + two pointers, skipping…
- What is the time complexity of this Java method? java String build(int n) { String s = ""; for (int i = 0; i < n; i++) s = s + "a"; return…