Interview Question

Anonymous Interview Seattle, WA

Given an array and

a sum, determine if any of the items add up to sum. Do a linear space solution and constant time solution.

Interview Answer

2 Answers


Tell me your constant time solution, because I am too noob to figure it out

needajob on Feb 22, 2012

This will return a list of the two numbers that add up to the sum

        public List<int> GetNumbers(int sum, List<int> array)
            var hashSet = new HashSet<int>();
            foreach (int i in array)
                if (hashSet.Contains(sum - i))
                    return new List<int> {i, sum - i};


            throw new Exception("not in here");

Anonymous on Aug 13, 2012

Add Answers or Comments

To comment on this question, Sign In with Facebook or Sign Up