Data Structures & AlgorithmsHardAmazon · Microsoft OA
Edge case: identity versus equality in Python.
Edge case: identity versus equality in Python.
a = [1, 2]; b = [1, 2]; c = a print(a == b, a is b, a is c, [] is [])What is printed?
- A. True True True True
- B. True False True True
- C. True False True False
- D. False False True False
Correct answer: C. True False True False
== compares values (equal lists), while is compares object identity: a and b are different objects, c is another name for a, and each [] literal creates a new list.
More Coding Interview Problems questions
- Edge case: chained comparisons and booleans as integers in Python. python print(1 < 2 > 1.5, (1 < 2) > 1.5, True + True, 3 > 2 == 2) What…
- Edge case: closures over a loop variable declared with var versus let in JavaScript. javascript const a = [], b = []; for (var i = 0; i <…
- Problem: Minimum number of swaps (any two positions) needed to sort an array of distinct values, by counting cycles of the permutation…
- Problem: Count subarrays whose sum lies in [lo, hi] using prefix sums and a sorted list (bisect). python from bisect import bisectleft…
- Problem: Starting at index s, you may jump from i to i + a[i] or i − a[i]. Can you reach any index holding 0? python def canreach(a, s)…
- Problem: Count non-empty subsequences whose minimum + maximum ≤ target (sort, then two pointers with powers of 2). python def count(a, t)…