Greedy on graph

Last updated: May 30, 2026

Quick Overview

Given a weighted directed graph, implement a greedy algorithm to find the minimum spanning tree (MST) using Prim's or Kruskal's method. The input will be a list of edges with their respective weights, and the output should be the total weight of the MST along with the edges included in it. Ensure your solution efficiently handles graphs with up to 10,000 vertices and edges.

Zillow
Coding & Algorithms
Software Engineer
Zillow
May 30, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

38

13

1,016 solved


Given a weighted directed graph, implement a greedy algorithm to find the minimum spanning tree (MST) using Prim's or Kruskal's method. The input will be a list of edges with their respective weights, and the output should be the total weight of the MST along with the edges included in it. Ensure your solution efficiently handles graphs with up to 10,000 vertices and edges.

This coding problem is frequently asked during Phone Screen at Zillow. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. Zillow expects candidates to write production-quality code, not just solve the puzzle.

What the Interviewer Expects
  • Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
  • Discuss multiple approaches and trade-offs before coding
  • Implement an optimal solution with clean, production-quality code
  • Handle all edge cases including boundary conditions and invalid input
  • Optimize both time and space complexity with clear justification
  • Test your solution systematically with well-chosen examples
Key Topics to Cover
Edge cases and input validation
Sorting and searching
Data structure selection and trade-offs
Binary search and divide and conquer
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would your solution change if the input was sorted?
  • Can you optimize the space complexity of your solution?
  • What if the input doesn't fit in memory?
  • Can you solve this iteratively instead of recursively (or vice versa)?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

The problem requires us to find the Minimum Spanning Tree (MST) of a weighted directed graph using a greedy algorithm. The most suitable algorithms for this task are Prim's and Kruskal's. Given that w...

Approach

We will implement Kruskal's algorithm to find the MST. The step-by-step algorithm is as follows:

  1. Sort all edges in non-decreasing order based on their weight.
  2. **Initialize a Union-Find (disj...

Submit Your Answer
Markdown supported

Related Questions