Data Structures & AlgorithmsVery hardICPC regional · Codeforces Div2 D
Problem: The permutation [2, 1, 4, 3] is processed by a single stack: input elements are pushed in order and popped whenever the popped element can be output next in sorted order (1, 2, 3, ...). Is the permutation sortable by this stack? Answer 1 for yes and 0 for no.
- A. 1
- B. 2
- C. 0
- D. 3
Correct answer: A. 1
A single stack sorts a permutation exactly when the greedy pop-when-next simulation empties the stack. Here 2 is pushed and 1 pops, so the stack handles both pairs and the permutation is sortable.
More Coding Interview Problems questions
- Problem: How many integers from 1 to 500 are coprime to 30 (share no prime factor with 30 = 2·3·5)?
- Problem: What is the sum of all prime numbers less than 30?
- Problem: How many bits are needed to write 10^9 in binary (the number of bits in its binary representation)?
- Problem: Reverse the 8-bit binary representation of 13 (00001101) and report the resulting unsigned value in decimal.
- Problem: What is the smallest positive integer x such that 1 + 2 + … + x is at least 100?
- Problem: How many composite numbers lie in the range 2 to 50 (inclusive)?