Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given a 0-indexed integer array nums of size 3 which can form the sides of a triangle.
Return a string representing the type of triangle that can be formed or "none" if it cannot form a triangle.
Example 1:
Input: nums = [3,3,3] Output: "equilateral" Explanation: Since all the sides are of equal length, therefore, it will form an equilateral triangle.
Example 2:
Input: nums = [3,4,5] Output: "scalene" Explanation: nums[0] + nums[1] = 3 + 4 = 7, which is greater than nums[2] = 5. nums[0] + nums[2] = 3 + 5 = 8, which is greater than nums[1] = 4. nums[1] + nums[2] = 4 + 5 = 9, which is greater than nums[0] = 3. Since the sum of the two sides is greater than the third side for all three cases, therefore, it can form a triangle. As all the sides are of different lengths, it will form a scalene triangle.
Constraints:
nums.length == 31 <= nums[i] <= 100Problem summary: You are given a 0-indexed integer array nums of size 3 which can form the sides of a triangle. A triangle is called equilateral if it has all sides of equal length. A triangle is called isosceles if it has exactly two sides of equal length. A triangle is called scalene if all its sides are of different lengths. Return a string representing the type of triangle that can be formed or "none" if it cannot form a triangle.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[3,3,3]
[3,4,5]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3024: Type of Triangle
class Solution {
public String triangleType(int[] nums) {
Arrays.sort(nums);
if (nums[0] + nums[1] <= nums[2]) {
return "none";
}
if (nums[0] == nums[2]) {
return "equilateral";
}
if (nums[0] == nums[1] || nums[1] == nums[2]) {
return "isosceles";
}
return "scalene";
}
}
// Accepted solution for LeetCode #3024: Type of Triangle
func triangleType(nums []int) string {
sort.Ints(nums)
if nums[0]+nums[1] <= nums[2] {
return "none"
}
if nums[0] == nums[2] {
return "equilateral"
}
if nums[0] == nums[1] || nums[1] == nums[2] {
return "isosceles"
}
return "scalene"
}
# Accepted solution for LeetCode #3024: Type of Triangle
class Solution:
def triangleType(self, nums: List[int]) -> str:
nums.sort()
if nums[0] + nums[1] <= nums[2]:
return "none"
if nums[0] == nums[2]:
return "equilateral"
if nums[0] == nums[1] or nums[1] == nums[2]:
return "isosceles"
return "scalene"
// Accepted solution for LeetCode #3024: Type of Triangle
/**
* [3024] Type of Triangle
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn triangle_type(nums: Vec<i32>) -> String {
if nums[0] + nums[1] <= nums[2]
|| nums[0] + nums[2] <= nums[1]
|| nums[1] + nums[2] <= nums[0]
{
return "none".to_string();
}
if nums[0] == nums[1] || nums[0] == nums[2] || nums[1] == nums[2] {
if nums[0] == nums[1] && nums[1] == nums[2] {
"equilateral".to_string()
} else {
"isosceles".to_string()
}
} else {
"scalene".to_string()
}
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3024() {
assert_eq!("equilateral", Solution::triangle_type(vec![3, 3, 3]));
assert_eq!("scalene", Solution::triangle_type(vec![3, 4, 5]));
}
}
// Accepted solution for LeetCode #3024: Type of Triangle
function triangleType(nums: number[]): string {
nums.sort((a, b) => a - b);
if (nums[0] + nums[1] <= nums[2]) {
return 'none';
}
if (nums[0] === nums[2]) {
return 'equilateral';
}
if (nums[0] === nums[1] || nums[1] === nums[2]) {
return 'isosceles';
}
return 'scalene';
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.