Amazon.com

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

Amazon.com Software Design Engineer In Test Interview Question

I interviewed in Seattle, WA and was asked:
"Roman to Int"
Add Tags [?]
Answer

Part of a Software Design Engineer In Test Interview Review - one of 4,774 Amazon.com Interview Reviews

Answers & Comments

0
of 2
votes

create a dictionary of std. roman values, parse through the roman string and match with the dictionary values -- keep incrementing the output as you iterate and keep truncating the input string as you go through

- Interview Candidate on Jul 28, 2012
1
of 1
vote

Your answer is incorrect. This is how you should have done it:

    public static int romanToLatin(String romanNumber) throws Exception
    {
        HashMap<String, Integer> romanLetters = new HashMap<String, Integer>();
        romanLetters.put("I", 1);
        romanLetters.put("V", 5);
        romanLetters.put("X", 10);
        romanLetters.put("L", 50);
        romanLetters.put("C", 100);
        romanLetters.put("D", 500);
        romanLetters.put("M", 1000);
        int stringLength = romanNumber.length() - 1;

        int result = 0;
        int last = 0;
        int sign = 1;
        int current;

        for (int i = stringLength; i >= 0; i--)
        {
            current = romanLetters.get(romanNumber.subSequence(i, i+1));
            if (current > last)
            {
                result += current;
                sign = 1;
            }
            else if (current == last)
            {
                result += current * sign;
            }
            else
            {
                result -= current;
                sign = -1;
            }
            last = current;
        }
        return result;
    }

- Ozan Eren Bilgen on Oct 19, 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.