Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1697: Checking Existence of Edge Length Limited Paths

In this guide, we solve Leetcode #1697 Checking Existence of Edge Length Limited Paths in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

An undirected graph of n nodes is defined by edgeList, where edgeList[i] = [ui, vi, disi] denotes an edge between nodes ui and vi with distance disi. Note that there may be multiple edges between two nodes.

Quick Facts

  • Difficulty: Hard
  • Premium: No
  • Tags: Union Find, Graph, Array, Two Pointers, Sorting

Intuition

The constraints hint that we can reason about two ends of the data at once, which is perfect for a two-pointer scan.

Moving one pointer at a time keeps the invariant intact and avoids nested loops.

Approach

Place pointers at the left and right ends and move them based on the comparison or target condition.

This yields a clean linear pass after any required sorting.

Steps:

  • Set left and right pointers.
  • Move a pointer based on the condition.
  • Update the best answer while scanning.

Example

Input: n = 3, edgeList = [[0,1,2],[1,2,4],[2,0,8],[1,0,16]], queries = [[0,1,2],[0,2,5]] Output: [false,true] Explanation: The above figure shows the given graph. Note that there are two overlapping edges between 0 and 1 with distances 2 and 16. For the first query, between 0 and 1 there is no path where each distance is less than 2, thus we return false for this query. For the second query, there is a path (0 -> 1 -> 2) of two edges with distances less than 5, thus we return true for this query.

Python Solution

class Solution: def distanceLimitedPathsExist( self, n: int, edgeList: List[List[int]], queries: List[List[int]] ) -> List[bool]: def find(x): if p[x] != x: p[x] = find(p[x]) return p[x] p = list(range(n)) edgeList.sort(key=lambda x: x[2]) j = 0 ans = [False] * len(queries) for i, (a, b, limit) in sorted(enumerate(queries), key=lambda x: x[1][2]): while j < len(edgeList) and edgeList[j][2] < limit: u, v, _ = edgeList[j] p[find(u)] = find(v) j += 1 ans[i] = find(a) == find(b) return ans

Complexity

The time complexity is O(m×log⁡m+q×log⁡q)O(m \times \log m + q \times \log q)O(m×logm+q×logq), where mmm and qqq are the number of edges and queries, respectively. The space complexity is O(1).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy