0% found this document useful (0 votes)
19 views3 pages

Java Metro Graph Interview QA

The document outlines the Java Metro Graph Project, detailing the data structures used to represent the graph, which is implemented as an undirected graph using a HashMap. It discusses various methods and their complexities, such as adding/removing vertices and edges, pathfinding, and potential modifications for directed graphs and live tracking. Additionally, it covers user interface options, input validation, and caching strategies for computed paths.

Uploaded by

akashmishra7006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Java Metro Graph Interview QA

The document outlines the Java Metro Graph Project, detailing the data structures used to represent the graph, which is implemented as an undirected graph using a HashMap. It discusses various methods and their complexities, such as adding/removing vertices and edges, pathfinding, and potential modifications for directed graphs and live tracking. Additionally, it covers user interface options, input validation, and caching strategies for computed paths.

Uploaded by

akashmishra7006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Metro Graph Project - Interview Q&A

1. What data structures are used to represent the graph?

The graph is implemented using a HashMap<String, Vertex> called vtces. Each vertex is an object

of the Vertex class, which contains another HashMap<String, Integer> called nbrs. This inner map

stores adjacent vertices (neighbors) and the cost (distance/time) to reach them. This structure

efficiently supports fast lookups, insertions, and deletions.

2. Is the graph directed or undirected? How can you tell?

The graph is undirected. This can be observed in the addEdge() method, where an edge between

two vertices is added in both directions.

3. What is the time complexity of adding or removing vertices and edges?

Add Vertex: O(1), Add Edge: O(1), Remove Vertex: O(V) due to neighbor cleanup, Remove Edge:

O(1).

4. What does hasPath() method do and how does it work?

It performs a Depth-First Search (DFS) to determine whether a path exists between two vertices,

using a processed map to avoid revisiting.

5. How would you modify the code to make the graph directed?

Remove the second line in addEdge() that adds the reverse edge. This ensures the edge is only

one-way.

6. Explain the purpose of the Vertex and DijkstraPair inner classes.

Vertex holds adjacent stations and distances. DijkstraPair is used during Dijkstras algorithm to track

vertex name, path so far, and cost.

7. Why is compareTo() overridden in DijkstraPair?

To define a custom comparison based on cost, so that objects can be correctly ordered in a priority

queue.
8. What is the difference between static and non-static in the code?

vtces is static, meaning it's shared across instances and accessible in static methods. Non-static

methods act on instance-level data.

9. Why are HashMap, ArrayList, and LinkedList used?

HashMap provides O(1) lookups, ArrayList stores dynamic lists like station names, LinkedList is

used for stack/queue operations in traversals.

10. How is the code modular or organized?

Each task like displaying stations or finding paths is encapsulated in its own method, promoting

clean design.

11. How would you modify this program to support live metro tracking?

Use APIs or GPS data, add timestamps, and use a GUI for real-time visual updates.

12. Is this implementation scalable for a large metro network?

To scale, store data persistently, use advanced algorithms like A*, and optimize memory usage.

13. How would you modify the code to find all possible paths?

Use recursive DFS with backtracking to explore and store every valid path.

14. Can the interchange-finding logic be optimized?

Yes, by using route/line IDs instead of relying solely on string manipulation.

15. How would you cache computed paths?

Use a HashMap<Pair<source, dest>, result> to store previously computed paths for reuse.

16. What features does the command-line interface (CLI) provide?

Users can view stations, metro map, shortest distance/time, path with interchanges, and exit the

app.

17. How is user input validated?

By using try-catch for input parsing and logical checks like containsVertex() and hasPath().

18. How would you convert this CLI to a GUI?


Use JavaFX or Swing, with dropdowns, panels, and possibly a map API for visualization.

19. What is the purpose of StringTokenizer in printCodelist()?

It helps generate short codes for station names, improving user experience when entering inputs.

20. Why is System.exit(0) used?

It terminates the program cleanly when the user chooses to exit.

21. Why is a processed HashMap needed in traversal algorithms?

To avoid revisiting nodes, prevent cycles, and improve correctness and efficiency.

You might also like