Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: What does this snippet print? s = "abcdefgh" print(s[::-3], s[5:1:-2])
- A. beh fd
- B. heb fd
- C. heb df
- D. hec fd
Correct answer: B. heb fd
Negative steps start from the end and move backward; the stop index is exclusive. Starting the reversed slice at index 1 or excluding the stop boundary changes the output.
More Coding Interview Problems questions
- Problem: What is the value printed by this snippet? from collections import Counter print(Counter("abracadabra").mostcommon(2))
- Problem: Consider the list a = [2, 4, 6, 8]. The loop 'for x in a: if x % 2 == 0: a.remove(x)' is run. What is the list a afterwards?
- Problem: How many recursive calls are made in total when naive Fibonacci fib(n) = fib(n−1) + fib(n−2) with fib(0) = fib(1) = base cases…
- Problem: What is printed? print(f"{3.14159:.2f}", f"{42:05d}", f"{0.5:.0%}")
- Problem: In Python, what is the value of (−5 >> 1) + 10, where >> is the arithmetic right shift applied to the negative integer −5?
- Problem: Records (name, score) are sorted by score descending with Python's stable sort…