{"record":{"id":"6695a3dcd5ce6e7b","repo":"mission-peace/interview","slug":"no-vertex-in-the-graph","errorCode":null,"errorMessage":"No vertex in the graph","messagePattern":"No vertex in the graph","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/com/interview/graph/CloneDirectedGraph.java","lineNumber":26,"sourceCode":" * Test cases\n * Graph is directed/non directed\n * Graph has 0 edges\n * Graph has cycle\n * Graph is linear\n * Graph is dense\n * Graph is sparse\n */\npublic class CloneDirectedGraph<T> {\n\n    public Graph<T> clone(Graph<T> graph){\n        if(graph == null){\n            return null;\n        }\n        if(!graph.isDirected){\n            throw new IllegalArgumentException(\"Cloning non directed graph\");\n        }\n        if(graph.getAllVertex().size() == 0){\n            throw new IllegalArgumentException(\"No vertex in the graph\");\n        }\n        Map<Vertex<T>,Vertex<T>> cloneMap = new HashMap<Vertex<T>,Vertex<T>>();\n        for(Vertex<T> vertex : graph.getAllVertex()){\n            clone(vertex,cloneMap);\n        }\n        Graph<T> clonedGraph = new Graph<>(true);\n        for(Vertex<T> vertex : cloneMap.values()){\n            clonedGraph.addVertex(vertex);\n        }\n        return clonedGraph;\n    }\n    \n    private void clone(Vertex<T> origVertex,Map<Vertex<T>,Vertex<T>> cloneMap){\n        Vertex<T> cloneVertex = null;\n        if(cloneMap.containsKey(origVertex)){\n            cloneVertex = cloneMap.get(origVertex);\n        }else{\n            cloneVertex = new Vertex<T>(origVertex.getId()*10);","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/mission-peace/interview/blob/94be5deb0c0df30ade2a569cf3056b7cc1e012f4/src/com/interview/graph/CloneDirectedGraph.java#L8-L44","documentation":"clone() refuses to clone a graph with zero vertices, throwing IllegalArgumentException(\"No vertex in the graph\"). An empty graph is treated as an invalid input rather than producing an empty clone, likely because the algorithm assumes a non-empty vertex set.","triggerScenarios":"Calling clone(graph) where graph.getAllVertex().size() == 0, e.g. a freshly constructed graph with no addVertex/addEdge calls, or a graph whose vertices were all removed.","commonSituations":"Cloning before graph construction completed, a deserialization step that produced an empty graph, or filtering that removed every vertex upstream.","solutions":["Populate the graph with vertices before cloning","Skip cloning when the graph is empty and return an empty Graph directly","Guard the call site: if (graph.getAllVertex().isEmpty()) handle separately"],"exampleFix":"// before\nGraph<T> copy = cloner.clone(maybeEmptyGraph);\n// after\nGraph<T> copy = maybeEmptyGraph.getAllVertex().isEmpty()\n    ? new Graph<>(true)\n    : cloner.clone(maybeEmptyGraph);","handlingStrategy":"validation","validationCode":"if (graph == null || graph.getAllVertex().isEmpty()) {\n    return new Graph<T>(true);\n}","typeGuard":null,"tryCatchPattern":"try {\n    Graph<T> copy = cloner.clone(graph);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"No vertex\")) copy = new Graph<>(true);\n}","preventionTips":["Check getAllVertex().isEmpty() before clone","Ensure graph-building code completes before cloning (log vertex count)","Treat empty-graph clones as a valid no-op in application code"],"tags":["java","graph","argument-validation"],"backgroundTag":"empty-required-field","analyzedSha":"94be5deb0c0df30ade2a569cf3056b7cc1e012f4","analyzedAt":"2026-09-08T13:27:05.954Z","contentChangedAt":"2026-09-08T13:27:05.954Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}