Data Structures & AlgorithmsVery hardGoogle · Flipkart interview
Edge case: JavaScript type coercion.
Edge case: JavaScript type coercion.
console.log(typeof null, [] + {}, [] == ![], 0.1 + 0.2 === 0.3);What is printed?- A. object [object Object] false false
- B. null [object Object] true false
- C. object true true
- D. object [object Object] true false
Correct answer: D. object [object Object] true false
typeof null is 'object' (a historical quirk). [] + {} converts both to strings: '' + '[object Object]'. ![] is false; [] == false converts both sides to numbers (0 == 0), so true. 0.1 + 0.2 is 0.30000000000000004 in binary floating point.
More Coding Interview Problems questions
- Edge case: += versus + on a list that has another name. python a = [1, 2, 3] b = a a += [4] a = a + [5] print(b, a) What is printed?
- 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?