Data Structures & AlgorithmsModerateProduct Company SDE · Amazon/Microsoft-style OA
Problem: Reverse the order of words in a sentence, collapsing multiple spaces.
Problem: Reverse the order of words in a sentence, collapsing multiple spaces.
s = " the sky is blue "
print("|" + " ".join(reversed(s.split())) + "|", len(s.split(" ")))What is printed?- A. |blue is sky the| 4
- B. | blue is sky the | 10
- C. |blue is sky the| 10
- D. |blue is sky the| 10
Correct answer: C. |blue is sky the| 10
split() with no argument splits on runs of whitespace and drops empty strings, so the words are the, sky, is, blue. split(" ") splits on every single space and keeps empty strings: 10 pieces. Interviewers check that you handle leading, trailing and repeated spaces.
More Coding Interview Problems questions
- Problem: Find the first non-repeating character in a string of lowercase letters; return its index or −1. java static int…
- Problem: Check whether s2 contains a permutation of s1 as a substring. python def hasperm(s1, s2): k = len(s1) need = [0] 26; win = [0] 26…
- Problem: Return the longest common prefix of a list of file paths. python def lcp(words): if not words: return "" p = words[0] for w in…
- Problem: Compute the minimum number of characters to add at the end of s so that it becomes a palindrome, using the KMP prefix function…
- Problem: Search a pattern of length m in a text of length n = 10⁶ (m up to 10⁵) and report all match positions. Which method guarantees…
- Problem: Return the longest palindromic substring of s (first one if there is a tie). python def lps(s): best = "" for c in range(len(s))…