Google | Onsite | Is graph a Grid ?

Give a undirected graph as a list of edges [(1, 2), (1, 3), (1, 4), (2, 3)], check if the graph forms a grid?
For examples,

True Case: [(1, 2), (1, 4), (2, 3), (2, 5), (3, 6), (4, 5), (5, 6)]

Because we can form grid,

1 2 3
4 5 6

False Case: [(1, 2), (1, 4), (2, 3), (2, 5), (3, 6), (4, 5)]

Because we cannot form grid,

1 2 3
4 5

Comments (6)