Data Structures & AlgorithmsHardTCS NQT · Infosys coding round

Problem: What does this Python snippet print? def add(x, bucket=[]): bucket.append(x) return len(bucket) add(1); add(2) print(add(3))

  1. A. 3
  2. B. 4
  3. C. 2
  4. D. 5

Correct answer: A. 3

A default list is created once and shared across calls, so it keeps growing. Assuming a fresh empty list per call gives 1 instead.

More Coding Interview Problems questions

Exams that ask this topic