Data Structures & AlgorithmsVery hardAmazon · Microsoft coding round
Problem: A bipartite graph check is commonly done by attempting a 2-coloring via BFS/DFS. What does finding an edge between two same-colored nodes indicate?
- A. The graph is not bipartite (an odd-length cycle exists)
- B. The graph has a negative cycle
- C. The graph is disconnected
- D. The graph must be a tree
Correct answer: A. The graph is not bipartite (an odd-length cycle exists)
If BFS/DFS ever tries to color a neighbour the same as its parent along the 2-coloring process, that edge forces two nodes at the same 'side' to be adjacent, which can only happen if some cycle in the graph has odd length -- exactly the condition that rules out bipartiteness.
More Coding Interview Problems questions
- Problem: What is the time complexity of finding the lowest common ancestor (LCA) of two nodes in a binary tree with n nodes, using a…
- Problem: With O(n) preprocessing (Euler tour + sparse table for range-minimum query), LCA queries on a tree of n nodes can be answered in…
- Problem: Which statement correctly describes the diameter of a tree (the longest path between any two nodes) and how to compute it…
- Problem: A min-heap (binary heap) supports insert and extract-min in what time complexity, and what is the time to build a heap from n…
- Problem: To find the k largest elements among n numbers, which approach is asymptotically best when k is much smaller than n?
- Problem: An LRU (Least Recently Used) cache with O(1) get and put operations is classically implemented with which combination of data…