Amazon.com

  www.amazon.com
Work in HR? Unlock Free Profile

Amazon.com 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 [?]
Answer

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

Answers & Comments

1
of 5
votes

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

- needajob on Feb 22, 2012
4
of 5
votes

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};

                hashSet.Add(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.