What is a graph?
Why Interviewers Ask This
This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex Data Structures & Algorithms topics. It also reveals how well you can explain technical ideas to non-experts.
Answer
A graph is a non-linear data structure consisting of vertices (nodes) and edges (connections between nodes). Formally G = (V, E). Types: Undirected — edges have no direction (Facebook friends); Directed (digraph) — edges have direction (Twitter follows, website links); Weighted — edges have a numerical weight/cost (road distances, flight costs); Unweighted — edges are equal; Cyclic — contains at least one cycle; Acyclic — no cycles; DAG (Directed Acyclic Graph) — directed, no cycles (dependency graphs, task scheduling). Representations: (1) Adjacency Matrix: V×V matrix, adj[i][j]=1 if edge exists. Space: O(V²). Edge lookup O(1), but wastes space for sparse graphs; (2) Adjacency List: array of lists, each containing neighbors. Space: O(V+E). Most common for sparse graphs; (3) Edge List: list of all edges as pairs. Useful for algorithms like Kruskal's. Real-world graphs: social networks, internet routing, maps, dependencies, recommendation systems.
Pro Tip
Before answering, structure your response: one-line definition → real-world analogy → concrete example from a project. This makes even complex Data Structures & Algorithms answers easy to follow.