Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: Nine nodes 0..8 start disconnected. Apply these unions in order (a union of two nodes already in one set does nothing): union(0,1), union(2,3), union(1,3), union(4,5), union(6,7), union(5,7), union(3,7). How many connected components remain?
- A. 3
- B. 2
- C. 4
- D. 1
Correct answer: B. 2
Seven unions each join two different sets, so the count falls from 9 to 9 - 7 = 2. Node 8 stays alone and the other eight nodes form one set after union(3,7). Forgetting the last union gives the trap answer 3.
More Coding Interview Problems questions
- 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)?
- Problem: Count the integers x with 1 <= x <= 1000 whose decimal digits sum to exactly 10.
- Problem: Modulo the prime p = 998244353, which value is the multiplicative inverse of 2, i.e. the x in [0, p) with 2x ≡ 1 (mod p)?
- Problem: A product of 11 matrices A1 A2 ... A11 is evaluated by choosing a full parenthesisation. How many different parenthesisations are…
- Problem: n = 10 people stand in a circle and every 3rd person is removed until one remains (counting starts at person 1 and the removed…