{"record":{"id":"2bdef693a5ad2a97","repo":"mission-peace/interview","slug":"cloning-non-directed-graph","errorCode":null,"errorMessage":"Cloning non directed graph","messagePattern":"Cloning non directed graph","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/com/interview/graph/CloneDirectedGraph.java","lineNumber":23,"sourceCode":"\n/**\n * Given a directed graph clone it in O(n) time where n is total number of edges\n * 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)){","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/mission-peace/interview/blob/94be5deb0c0df30ade2a569cf3056b7cc1e012f4/src/com/interview/graph/CloneDirectedGraph.java#L5-L41","documentation":"clone() only supports cloning directed graphs; it uses graph.isDirected as a precondition check. If a graph whose isDirected flag is false is passed, it throws IllegalArgumentException(\"Cloning non directed graph\") because the clone routine builds a Graph<>(true) and assumes directed edge semantics.","triggerScenarios":"Calling clone(graph) with a graph created via new Graph<>(false) or any undirected Graph instance.","commonSituations":"Passing a graph built by a different factory/constructor default, or reusing an undirected graph utility where a directed one was expected after an API change.","solutions":["Construct the graph as directed (new Graph<>(true)) before cloning","If undirected cloning is needed, implement or use a separate clone routine for undirected graphs","Check graph.isDirected before calling and branch to the appropriate cloner"],"exampleFix":"// before\nGraph<T> copy = cloner.clone(undirectedGraph);\n// after\nif (undirectedGraph.isDirected) {\n    Graph<T> copy = cloner.clone(undirectedGraph);\n} else {\n    throw new UnsupportedOperationException(\"use undirected clone\");\n}","handlingStrategy":"type-guard","validationCode":"if (!graph.isDirected) {\n    throw new UnsupportedOperationException(\"clone requires a directed graph\");\n}","typeGuard":"boolean isClonable(Graph<T> g) { return g != null && g.isDirected; }","tryCatchPattern":"try {\n    Graph<T> copy = cloner.clone(graph);\n} catch (IllegalArgumentException e) {\n    throw new IllegalStateException(\"undirected graph passed to directed clone\", e);\n}","preventionTips":["Always construct graphs used with this cloner via new Graph<>(true)","Centralize graph creation in one factory so the directed flag is consistent","Add a unit test asserting clone throws for undirected input"],"tags":["java","graph","argument-validation"],"backgroundTag":"unsupported-operation","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"}