Skip to content
QuizMaker logoQuizMaker
Activity
DSA Interview Patterns Roadmap

No lessons available

CONTENTS

Antenna Combinations Within Distance

Turn the Antenna Combinations Within Distance interview variant into a clear brute-force baseline, optimized pattern, and implementation plan.

DSA Interview Patterns Roadmap
Company Asked Variants
dsa
coding interview
+5
May 29, 2026
19
A

Learning Outcome

Turn the Antenna Combinations Within Distance interview variant into a clear brute-force baseline, optimized pattern, and implementation plan.

Original Interview Statement

Given antenna positions and distance d, count valid pairs or combinations whose difference is less than d.

Examples

ItemDetail
positions=[1,3,5], d=3pairs (1,3),(3,5)

Brute Force Approach

Check every pair or every triple directly.

Optimized Approach

Sort positions and use two pointers. For each right, move left until distance is valid, then add combinations from the window.

Exact Pseudocode

sort a
left = 0
for right in range(n):
  while a[right] - a[left] >= d: left += 1
  add right-left valid pairs

Reference Code

def count_pairs_within_distance(pos, d):
    pos.sort()
    left = 0
    ans = 0
    for right, value in enumerate(pos):
        while value - pos[left] >= d:
            left += 1
        ans += right - left
    return ans

Complexity

ItemDetail
Brute forceO(n^2)
OptimizedO(n log n) sorting plus O(n)

Edge Cases

  • Equal positions
  • Strict < d vs <= d
  • Need triples instead of pairs

Follow-ups

  • Count triples
  • Return actual pairs

Nearest Practice References

  • Two pointers counting pairs

Common Mistakes

  • Copying the nearest LeetCode solution without checking the changed rule.
  • Skipping duplicate or boundary cases.
  • Not stating the brute force before the optimized approach.

Share this article

Test your knowledge

Take a quick quiz based on this chapter.

hardDSA Interview Patterns
Quiz: Antenna Combinations Within Distance
6 questions10 min

0 comments

Please login to comment.
No comments yet.
Lesson 12 of 13 in Company Asked Variants
Previous in Company Asked Variants
Circle Group Connectivity
Next in Company Asked Variants
Knockout Tournament Validation
Back to DSA Interview Patterns Roadmap
Back to moduleCategories