Linkedin Interview Question for Interns


Country: United States
Interview Type: Phone Interview




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

public class romanNumeral {

	public static void main(String[] args) {
		
		//Dictionary to store Roman Numeral
		Map<Character, Integer> map = new HashMap<>();
		map.put('I', 1);
		map.put('V', 5);
		map.put('X', 10);
		map.put('L', 50);
		map.put('C', 100);
		map.put('D', 500);
		map.put('M', 1000);
		
		//Let's consider this example
		String str = "IX";
		
		//Initialize count to 0
		int count = 0;
		for(int i =0;i<str.length() - 1;i++){
			if(map.get(str.charAt(i)) > map.get(str.charAt(i + 1))){
				count = count + map.get(str.charAt(i));
			} 
			else if(map.get(str.charAt(i)) == map.get(str.charAt(i+1)) ){
					if(map.get(str.charAt(i))%10 == 5 ){
				//Something like this "VV" or "LL"
				System.out.println("Invalid number");
				break;
			} else{
				count = count + map.get(str.charAt(i));
				}
			}
			else{
				count = count + (map.get(str.charAt(i+1)) - map.get(str.charAt(i)));
				i++;
			}
		}
		System.out.println(count);
	}
}

- krishnamurthymegha January 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The space can be improved a small bit, but this is a practical solution.

public class Roman {

    private static Map<Character, Integer> lookupMap = new HashMap<>();

    static {
        lookupMap.put('I', 1);
        lookupMap.put('V', 5);
        lookupMap.put('X', 10);
        lookupMap.put('L', 50);
        lookupMap.put('C', 100);
        lookupMap.put('D', 500);
        lookupMap.put('M', 1000);
    }

    public static void main(String[] args){
        String[] tests = new String[]{
                "IV",
                "XII",
                "XL",
                "XX",
                "LXXX",
                "LXL"
        };

        for(String s : tests){
            out.println(s);
            out.println(getInteger(s));
            out.println(getRoman(getInteger(s)));
            System.out.println();
        }
    }

    private static int getInteger(String roman){
        int sum = 0;

        int previous = 0;

        for(int i = roman.length()-1; i >= 0; i--){
            char c = roman.charAt(i);

            int value = lookupMap.get(c);

            if(previous > value){
                sum -= value;
            } else {
                sum += value;
            }

            previous = value;
        }

        return sum;
    }

    private static int[] values = new int[]{1000, 500, 100, 50, 10, 5, 1};
    private static char[] romans = new char[]{'M', 'D', 'C', 'L', 'X', 'V', 'I'};

    private static String getRoman(int integer){
        StringBuilder sb = new StringBuilder();
        int[] romanCount = new int[romans.length];

        for(int i = 0; i < values.length; i++) {
            int val = values[i];
            char roman = romans[i];

            int quotient = integer / val;

            for (int j = 0; j < quotient; j++) {
                romanCount[i]++;
            }

            integer -= quotient * val;
        }

        out.println(Arrays.toString(romanCount));

        for(int i = 0; i < romanCount[0]; i++){
            sb.append(romans[i]);
        }

        for(int i = 0; i < romanCount.length; i++){
            int count = romanCount[i];

            if(count == 4){
                sb.append(romans[i]);
                sb.append(romans[i-1]);
            } else {
                for(int j = 0; j < romanCount[i]; j++){
                    sb.append(romans[i]);
                }
            }
        }

        return sb.toString();
    }

}

- dev.yeison March 12, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def convertToInteger(roman):
        dic = {'I':1, 'V':5, 'X': 10, 'L': 50, 'C':100, 'D': 500, 'M': 1000}
        result = dic[roman[-1]]
        digit = dic[roman[0]]
        for i in range(1,len(roman)):
            next = dic[roman[i]]
            result += -digit if digit < next else digit
            digit = next
        return result

- assertNull April 12, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def convertToRoman(integer):
        digits, romans = [1000,500,100,50,10,5,1], ['M','D','C','L','X','V','I']
        result = ''
        for i, digit in enumerate(digits):
            repeat, integer= divmod(integer, digit)
            if repeat == 4:
                if result and result[-1] == romans[i-1]:
                    result = result[:-1] + romans[i] + romans[i-2]
                else: result += romans[i] + romans[i-1]
            else: result += repeat*romans[i]
        return result

- assertNull April 12, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def roman_to_int(s):
	roman_numerals =  {'I' : 1, 'V' : 5, 'X' : 10, 'L' : 50, 'C' : 100, 'D' : 100, 'M' : 1000}
	sum = 0
	l = []
	for i in range(len(s)):
		for k,v in roman_numerals.items():
			if l[i] == k:
				l.append(v)
	l.append(0)
	for i in range(len(s)):
		if l[i] >= l[i + 1]:
			value = value + l[i]
		else:
			value = value - l[i]

	return value

- Anonymous June 14, 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