Lesson overview · Free interview practice

Minimum Spanning Trees

Find the subset of edges that connects all vertices with the minimal total weight.

Topics in the full lesson
  • Concept and Use Cases
  • Key Concepts
  • Time and Space Complexity
  • MST Algorithms and Methods
  • Practical Tips and Tricks
  • Common Gotchas
  • Advanced Topics
  • Interview Tips and Tricks
  • Common Mistakes

Try the free interview questions

Work through a question before revealing its explanation. These questions and answers are free; Premium adds the full lesson walkthrough, examples and implementation detail.

Question

There are n cities labeled 1..n. You are given connections, where connections[i] = [a, b, cost] is the cost to build a bidirectional link between city a and city b. Return the minimum total cost to connect all n cities so that every city is reachable from every other. If it is impossible to connect them all, return -1.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Question

There are n houses labeled 1..n. You can supply water to a house either by building a well on it — wells[i - 1] is the cost of a well at house i — or by laying a pipe from another supplied house, where pipes[j] = [house1, house2, cost] is the cost of a bidirectional pipe (multiple pipes may connect the same pair). Return the minimum total cost to supply water to every house.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Explore the full Minimum Spanning Trees material

Premium includes the complete lessons and implementation references. Free practice questions remain available without a subscription.

All course tracks & premium content
From basics to advanced masterclasses
Built for JS/TS developers like you
Real-world tips & common pitfalls
Upgrade to Premium

Continue in this section