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.

Summary

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.

Examples

[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.

Solution

Can be found on the solutions branch on github.

Jordan Hudgens

view all post
Leave a comment

Please be polite. We appreciate that.

By Daniele Zedda • 18 February

← PREV POST

By Daniele Zedda • 18 February

NEXT POST → 34
Share on