Find All Triplets with Zero Sum in Ruby


This is first in the series of Google coding interview questions. In order to pass this exercise, you will need to know how to find all of the potential combinations for an array, along with selecting the values that match a calculated sum.


Find all of the potential 3 digit combinations inside of an array that have a sum of 0.

Exercise File

Code File

Exercise Description

Given an array of numbers, return an array of arrays that contain all of the potential 3 digit combinations that have a sum of 0.


[0, -1, 2, -3, 1].triplet_sum_zero # => [[0, -1, 1], [2, -3, 1]]
[1, -2, 1, 0, 5].triplet_sum_zero # => [[1, -2, 1]]

Real World Usage

This is a popular Google interview coding question. It forces you to work with collections of values and perform non-trivial calculations in order to return the correct elements.


Can be found on the solutions branch on github.


Please enter your comment!
Please enter your name here