Largest Perimeter Triangle

EasyGreedygreedy

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given an integer array nums of side lengths, choose three of them (distinct indices) that form a triangle with **positive area**, and return the largest possible perimeter. Three lengths a <= b <= c form a non-degenerate triangle exactly when a + b > c. If no three lengths work, return 0.

Examples

Input: nums = [2,1,2]
Output: 5
Sides 1, 2, 2 satisfy 1 + 2 > 2.
Input: nums = [1,2,1,10]
Output: 0
No triple satisfies the triangle inequality: 1 + 1 = 2, not > 2, and 10 is too long for any pair.

Constraints

  • 3 <= nums.length <= 10^4
  • 1 <= nums[i] <= 10^6

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More greedy problems