Work in HR? Unlock Free Profile Senior Software Engineer Interview Question

I interviewed in Seattle, WA and was asked:
"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."
Add Tags [?]

Part of a Senior Software Engineer Interview Review - one of 4,689 Interview Reviews

Answers & Comments

of 5

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

- needajob on Feb 22, 2012
of 5

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

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

Tags are like keywords that help categorize interview questions that have something in common.