Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft

Reverse the order of words, collapsing multiple spaces. What is printed?

Reverse the order of words, collapsing multiple spaces. What is printed?
s = "  oil   is   data   new "
w = s.split()
print("|" + " ".join(reversed(w)) + "|", len(s.split(" ")))
  1. A. |new data is oil| 4
  2. B. |new data is oil| 12
  3. C. |new data is oil| 13
  4. D. |new data is oil| 15

Correct answer: C. |new data is oil| 13

split() with no argument drops empty strings, but split(" ") keeps an empty string for every extra space. Output: |new data is oil| 13.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic