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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

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
Two Pointers
Sorting
Counting Pairs
Triangle Inequality
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: list lengths of non-negative integers.
  • Output: number of index triples i < j < k such 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]...


Submit Your Answer
Markdown supported