Pergunta de entrevista da empresa Microsoft

Standard coding and design questions. Some questions about my background. A tedious question was to convert a string representing a roman number to an integer. This question is not hard to solve, but tedious to code.

Respostas da entrevista

Sigiloso

23 de fev. de 2015

Not the complete solution, but it will be something like this: String[] units = {"", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix"}; String[][] tens = { {"10", "x"} }; String roman = "xxxviii"; int dec = 0; for(int i = 0; i < roman.length(); i++) { if(tens[0][1].equalsIgnoreCase(String.valueOf(roman.charAt(i)))) { dec += Integer.parseInt(tens[0][0]); } else { for(int j = 0; j < units.length; j++) { if(units[j].equalsIgnoreCase(String.valueOf(roman.charAt(i)))) { dec += j; } } } } System.out.println(dec);

Sigiloso

12 de set. de 2017

public class Roman { char symbols[] = {'M', 'D', 'C', 'L', 'X', 'V', 'I'}; int values[] = {1000, 500, 100, 50, 10, 5, 1}; public int romanToInt(String s) { if (s.length() == 0) return 0; s = s.toUpperCase(); for (int i = 0; i < symbols.length; i++) { int index = s.indexOf(symbols[i]); if (index != -1) { return values[i] - romanToInt(s.substring(0, index)) + romanToInt(s.substring(index + 1)); } } return 0; } }