Lesson overview · Free interview practice

Approximation Algorithms

Find near-optimal solutions for hard optimization problems.

Topics in the full lesson
  • Concept and Use Cases
  • Key Concepts
  • Time and Space Complexity
  • Approximation 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 is a one-dimensional garden on the x-axis between coordinates 0 and n. There are n + 1 taps located at integer positions [0, 1, 2, ..., n]; tap i waters area [i - ranges[i], i + ranges[i]]. Return the minimum number of taps that must be opened to water the entire garden, or -1 if it is impossible.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Question

Given an array positions where positions[i] = [xi, yi] are the 2D coordinates of customer locations, return the minimum possible sum of Euclidean distances from a chosen service-center point to all customer locations. Answers within 10^-5 of the actual value are accepted. (This is the geometric-median / Weber problem; solve iteratively, e.g., with Weiszfeld's algorithm or simulated annealing.)

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Explore the full Approximation Algorithms 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