Count Triples of Fiber Cable Lengths That Can Form a Triangle
Last updated: November 29, 2026
Quick Overview
Given lengths of fiber cable segments, count how many triples of different segments could form a non-degenerate triangle, meaning the two shorter lengths sum to more than the longest.
0
0
0 solved
Given lengths of fiber cable segments, count how many triples of different segments could form a non-degenerate triangle, meaning the two shorter lengths sum to more than the longest.
A Google technical-screen problem that looks like a triple loop but reduces to sorted two pointers that count many pairs at once. The interviewer checks whether the candidate fixes the largest side and adds `hi - lo` in one step.
What the Interviewer Expects
- Sorts and fixes the largest side
- Only checks `a + b > c` because the other inequalities hold automatically
- Counts `hi - lo` valid pairs in one step
- Handles zeros and duplicates
- States O(n^2) time
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you list the triangles rather than count them?
- What if you need triangles whose perimeter is at most P?
- Can you do better than O(n^2) if lengths are small integers?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: list
lengthsof non-negative integers. - Output: number of index triples
i < j < ksuch that the three lengths satisfy the strict triangle inequality....
High-Level Design
Sort ascending. Fix k as the index of the longest side, from the end. Then for c = a[k], look for pairs lo < hi < k with a[lo] + a[hi] > c. Start with lo = 0, hi = k - 1. If `a[lo] + a[hi]...