Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: Using a single counter, a sequence of commands is applied to the value x = 0: "+5", "×2", "−3", "×2", "+1", "÷4" (integer division, truncating toward zero), "×3", "−7". What is the final value of x?
- A. 3
- B. 2
- C. 1
- D. 4
Correct answer: B. 2
Sequential simulation with truncation toward zero for the division step. Python's floor division // gives a different result for negative intermediate values, so the sign matters here.
More Coding Interview Problems questions
- Problem: In a binary max-heap stored in an array with 1-based indexing, the array is [40, 30, 35, 10, 20, 25, 28, 5, 8, 12]. An operation…
- Problem: In the array [3, 8, 1, 9, 4, 7, 2, 6, 5], what is the length of the longest subarray whose sum is exactly 20?
- Problem: A sorted list 2, 4, 6, 8, 10, 12, 14 is treated as a rotated array: the first 3 values are moved to the end, giving 8, 10, 12…
- Problem: Four processes arrive at times 0, 2, 4, 6 with burst times 6, 2, 8, 3. The scheduler is shortest-remaining-time-first…
- Problem: In the grid of 0s and 1s below, count the number of 2×2 all-ones squares. 1110 1111 0111 1111
- Problem: A string s = "abacaba" is given. How many of its substrings are palindromes of odd length (counting index ranges, so repeated…