Can you find a triplet where a squared equals b squared plus c squared?

Coding
Medium
Amazon
89.5K views

This problem asks you to identify Pythagorean triplets within an array, often with variations involving sum zero. It tests mathematical reasoning and hash-based lookups.

Why Interviewers Ask This

Amazon interviewers use this to evaluate a candidate's ability to combine mathematical logic with efficient algorithmic design. They look for understanding of the two-pointer technique or hashing strategies to reduce time complexity from cubic to quadratic. The variation with sum zero further tests adaptability to similar problems like 3Sum.

How to Answer This Question

Clarify if the array is sorted; if not, explain sorting first. Discuss the brute-force O(N^3) approach briefly before optimizing. Propose fixing one element and using a hash set or two pointers to find the other two. For the squared condition, iterate through pairs and check if the square of the third exists in the set. Ensure you handle duplicates correctly and discuss the O(N^2) complexity improvement.

Key Points to Cover

  • Iterate through each element as the potential hypotenuse
  • Use Hash Set for O(1) lookups
  • Apply Two Pointer technique if sorted
  • Optimize from O(N^3) to O(N^2)

Sample Answer

I would start by iterating through each element to treat it as 'a'. Then, for every pair of remaining elements 'b' and 'c', I check if a*a equals b*b plus c*c. To optimize, I can use a hash set to store squares of all el…

Common Mistakes to Avoid

  • Forgetting to handle floating point precision issues
  • Missing duplicate triplet combinations
  • Failing to sort the array before using two pointers

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 50 Coding questionsBrowse all 155 Amazon questions