ProgrammingModerateProduct-company MCQ pattern
What is the time complexity of s[::-1] for a string of length n in CPython?
- A. O(1), because slices of strings are views
- B. O(log n), because it uses binary search
- C. O(n), because it builds a new reversed copy
- D. O(n squared), because each character is moved repeatedly
Correct answer: C. O(n), because it builds a new reversed copy
Strings are immutable and have no slice views, so the reversed result is a new string built in one pass. That costs O(n) time and O(n) extra memory.
More Python Programming questions
- Which call builds one string from n pieces in O(total length) time?
- Which statement about sorting in Python is correct?
- What is printed? python print(oct(-8), hex(-255), bin(0), format(-5, 'b'))
- What is printed? python n = 13 print(n ^ (n >> 1), [bin(i ^ (i >> 1)) for i in range(4)])
- What is printed? python print(~5 == -6, ~(-5) == 4, (-5) >> 1 == -2, -5 // 2 == -3)
- What is printed? python import unicodedata print(unicodedata.normalize('NFC', 'eu0301') == 'é', len(unicodedata.normalize('NFD', 'é'))…