ProgrammingVery hardAmazon OA · Flipkart coding round

What is printed?

What is printed?
def partition(s):
    last = {ch: i for i, ch in enumerate(s)}
    out = []
    start = end = 0
    for i, ch in enumerate(s):
        end = max(end, last[ch])
        if i == end:
            out.append(i - start + 1)
            start = i + 1
    return out
print(partition('ababcbacadefegdehijhklij'))
  1. A. [10, 7, 7]
  2. B. [9, 8, 7]
  3. C. [9, 7, 8, 0]
  4. D. [9, 7, 8]

Correct answer: D. [9, 7, 8]

A segment ends once every letter inside it has reached its last occurrence. The three segments have lengths 9, 7 and 8, which add up to the 24 characters.

More Python Programming questions

Exams that ask this topic