Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
1
of 1 vote

public class RomanNumber {

    private final static TreeMap<Integer, String> romanMap = new TreeMap<Integer, String>();

    static {

        romanMap.put(1000, "M");
        romanMap.put(900, "CM");
        romanMap.put(500, "D");
        romanMap.put(400, "CD");
        romanMap.put(100, "C");
        romanMap.put(90, "XC");
        romanMap.put(50, "L");
        romanMap.put(40, "XL");
        romanMap.put(10, "X");
        romanMap.put(9, "IX");
        romanMap.put(5, "V");
        romanMap.put(4, "IV");
        romanMap.put(1, "I");

    }

    public final static String convertToRoman(int number) {
        int l =  romanMap.floorKey(number);
        if ( number == l ) {
            return romanMap.get(number);
        }
        return romanMap.get(l) + convertToRoman(number-l);
    }
    public static void main(String[] args) {
    	for (int i = 1; i<= 110; i++) {
            System.out.println(i+"\t =\t "+RomanNumber.convertToRoman(i));
        }
	}

}

- mohsin May 26, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

To write up a method for converting integer to Roman numerals is relatively easy.

But I wasn't confident in how to design a class for it. 
'Cause there didn't seem like much.

I just answered, "It does a simple conversion that we can make it static and put it in an utility class". 

Any thoughts?

- CoderLonely May 25, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Not everyone is a big fan of statics. Especially cause of it getting in the way of unit testing.

I would put this simply this way.

public sealed class RomanNumeric
{
       public RomanNumeric()
       {
       }

       public string GetRomanNumeric(int x)
       {
           ...
       }

       //In case of C#, a syntactic sugar for converting int to roman
       public static implicit operator string (int x)
       {
           ...
       }
}

- Frank Q May 25, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public String intToRoman(int num) {
        int numbers[] = new int[]{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        String roman[] = new String[]{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        
        int index = 0, x = num;
        StringBuilder result = new StringBuilder();
        
        while(x != 0){
            int count = x / numbers[index];
            if(count > 0){
                while(count > 0){
                    result.append(roman[index]);
                    count--;
                }
                x = x % numbers[index];
            }
            index++;
        }
        
        return result.toString();
    }

- AJ June 24, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Writing the function in Javascript ...

function romanize(num){
var romanDecimal ={M:1000,CM:900,D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1};
var i;
var roman = '';

for (i in romanDecimal){
while(num >= romanDecimal[i]){
roman = roman+i;
num = num - romanDecimal[i];
}
}
return roman;
}

for ( var i=1 ; i <=10 ; ++i )
{
document.getElementById("demo").innerHTML+= i+"\t"+romanize(i)+"<br/>";
}

- Nidhi Sinha July 13, 2018 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More