Data Structures & AlgorithmsHardAmazon · Microsoft OA
Edge case: += versus + on a list that has another name.
Edge case: += versus + on a list that has another name.
a = [1, 2, 3] b = a a += [4] a = a + [5] print(b, a)What is printed?
- A. [1, 2, 3, 4] [1, 2, 3, 4, 5]
- B. [1, 2, 3] [1, 2, 3, 4, 5]
- C. [1, 2, 3, 4, 5] [1, 2, 3, 4, 5]
- D. [1, 2, 3, 4] [1, 2, 3, 4]
Correct answer: A. [1, 2, 3, 4] [1, 2, 3, 4, 5]
a += [4] extends the list IN PLACE, so b (the same object) sees 4. a = a + [5] builds a NEW list and rebinds a, leaving b unchanged.
More Coding Interview Problems questions
- Edge case: changing a dict while iterating over it. python d = {1: 'a', 2: 'b', 3: 'c'} try: for k in d: if k == 2: d[4] = 'd' print("ok"…
- Edge case: Python's round() and sorting strings with mixed case. python print(round(2.5), round(3.5), round(-0.5), sorted(['b', 'A', 'a'…
- Problem: Shops sit on a circular ring with profits p = [5, -3, 5, 4, -8, 6]. Choose a contiguous arc of at least one shop (the arc may…
- Problem: Nine nodes 0..8 start disconnected. Apply these unions in order (a union of two nodes already in one set does nothing)…
- Problem: How many prime numbers are there that are less than or equal to 10^6?
- Problem: Let phi(k) be Euler's totient (the count of integers g in [1, k] with gcd(g, k) = 1). What is phi(1) + phi(2) + ... + phi(10)?