Facebook Interview Question for Interns


Country: United States
Interview Type: Phone Interview




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

Roman numerals: see the wikipedia

const static string romanStr = "IVXLCDM";
const static int romanVal = {1, 5, 10, 50, 100, 500, 1000};

int GetRomanValue(string input)
{
    int total = 0, temp = 0;
    
    for (string::iterator it = input.rbegin(); it != input.rend(); ++ it){
        size t = romanString.find(*it);
        total += romanVal[t] > temp? romanVal[t] : -romanVal[t];
        temp = romanVal[t];
    }

    return total;
}

Suppose that "input" is a valid string of Roman characters.

- peter tang November 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this will return -4 for something like "IV". Am I missing something?

- Felix January 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What about this test case : "II" . I think your function will return 0

- ZuZu May 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Now that I've seen someone else's solution, I wish I had iterated over the string from right-to-left.

long parse_roman(string const &roman) {
        static map<char, long> values = {
                {'I', 1 },
                {'V', 5 },
                {'X', 10 },
                {'L', 50 },
                {'C', 100 },
                {'D', 500 },
                {'M', 1000 },
        };
        long total = 0;
        long lastdigit = 0;
        long lastrun = 0;
        for (auto i = roman.begin(); i != roman.end(); ++i) {
                auto value = values.find(*i);
                if (value == values.end()) {
                        ostringstream message;
                        message << "Illegal digit: " << *i;
                        throw runtime_error(message.str());
                }
                long digit = value->second;
                if (digit == lastdigit) {
                        lastrun += digit;
                } else {
                        if (digit < lastdigit) {
                                total += lastrun;
                        } else {
                                total -= lastrun;
                        }
                        lastdigit = lastrun = digit;
                }
        }
        total += lastrun;
        return total;
}

- Chad Parry November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Borth the above solutions are wrong, because they don't handle the case XLIV.

Proper solution starts from end of string and proceeds towards first char. add if the prev elem is greater than cur. subtract if prev is less than cur.

prevValue = 0;
curSum = 0;
for ( i= str.lenght(0-1; i >=0; i--) {
curElem = str.at(i);
curValue = valueMap.find(curElem)->second;
if ( prevValue > 0 && prevValue < curValue) {
curSum = curSum - curValue;
else {
curSum +=curValue;
}
prevValue = curValue;
}

return curSum;

- Anonymous January 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sorry the condition should be
if ( prevValue > 0 && prevValue > curValue) {
curSum = curSum - curValue;
else {
curSum +=curValue;
}

- Anonymous January 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String str = "MCMXLVII";
        
        HashMap<Character, Integer> roman = new HashMap<Character, Integer>();
        roman.put('M', 1000);
        roman.put('C', 100);
        roman.put('L', 50);
        roman.put('X', 10);
        roman.put('V', 5);
        roman.put('I', 1);
        
        char[] chars = str.toCharArray();

        int lastValue = Integer.MAX_VALUE;
        int total = 0;
        for(int i = 0; i < chars.length; i++)
        {
            char c = chars[i];
            int value = roman.get(c);
            if(value > lastValue)
                total += (value-lastValue*2);
            else
                total += value;
            lastValue = value;
        }
        System.out.println(total);

- Anonymous January 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int romans(string& s) {
	int s = 0;
	int anchor = 0;
	for(string::reverse_iterator it = s.rbegin(); it != s.rend(); ++it) {
		if (*it >= anchor) { s += *it; anchor = *it; }
		else { s -= *it; }
	}
	return s;
}

- Anonymous February 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def print_roman(s):
map ={'I': 1 ,'V': 5 ,'X': 10 ,'L': 50 ,'C': 100 ,'D': 500 ,'M': 1000 }
sum = 0
prev = None
for i in range(len(s)-1, -1, -1):
c = s[i]
val = map[c]
if not prev == None:
if prev > val:
sum -= val
else:
sum += val
else:
sum += val
prev = val
return sum

- Anonymous February 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What are the rules to convert?

This is a bad interview question. Some people might not even know roman numerals, and those more familiar have a distinct advantage. Hard to judge anything by this question.

- Anonymous February 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Who the fuck doesn't know what are roman numerals? Are you kidding me?

- Anonymous January 08, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int RomanToInteger(string s)
{
	int result = 0;	
	int d = 1;
	int prev = -1;

	for(int i = s.Length - 1; i >= 0; i--)
	{
		int curr = 0;

		switch(s[i])
		{
			case 'I':
				curr = 1;
				break;
			case 'V':
				curr = 5;
				break;
			case 'X':
				curr = 10;
				break;
			case 'L':
				curr = 50;
				break;
			case 'C':
				curr = 100;
				break;
			case 'D':
				curr = 500;
				break;
			case 'M':
				curr = 1000;
				break;
			default:
				throw new Exception("Invalid roman numeral");
		}

		if (prev == -1 || curr > prev) d = 1;
		else if (curr < prev) d= -1;

		result += d * curr;

		prev = curr;
	}

	return result;
}

- ZuZu May 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Actually this would Work:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package romantodecimal;

/**
 *
 * @author ASUS
 */
import java.util.Scanner;
public class RomanToDecimal {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       RomanToDecimal obj=new RomanToDecimal();
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter Roman:");
       System.out.println(obj.convert(sc.nextLine()));
    }
    public int convert(String s){
        String []r={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
        int []v={1000,900,500,400,100,90,50,40,10,9,5,4,1};
        int index=0;
        int result=0;
        while(!s.equals("")&&index<13){
            while(s.startsWith(r[index])){
                result+=v[index];
                s=s.substring(r[index].length());
            }
            index++;
        }
       return result;
    }
}

- Adarsh September 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This code will be ok.

int Roman2Int(std::string & str) {
  int rs = 0;
  int pre = 1001;
  int cur = 0;
  for (int i = 0; i < str.size(); i++) {
    if (str[i] == 'I') cur = 1;
    else if (str[i] == 'V') cur = 5;
    else if (str[i] == 'X') cur = 10;
    else if (str[i] == 'L') cur = 50;
    else if (str[i] == 'C') cur = 100;
    else if (str[i] == 'D') cur = 500;
    else cur = 1000;
    if (cur <= pre) rs += cur;
    else rs = rs - pre + (cur - pre);
    pre = cur;
  }
  return rs;
}

- guoliqiang2006 January 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My solution

package Other;

import java.util.HashMap;

public class Roman {

	public static int roman2Decimal(String roman) {
		HashMap<Character, Integer> values = new HashMap<Character, Integer>();
		values.put('M', 1000);
		values.put('C', 100);
		values.put('L', 50);
		values.put('X', 10);
		values.put('V', 5);
		values.put('I', 1);

		int value = 0;
		for (int index = 0; index < roman.length();) {
			int tmp = 0;
			char current = roman.charAt(index);
			do {
				tmp += values.get(current);
				index++;
			} while( index + 1 < roman.length() && roman.charAt(index) == current);

			if (index < roman.length()
					&& values.get(current) < values.get(roman.charAt(index))) {
				tmp *= -1;
			}
			value += tmp;
		}
		return value;
	}

	public static void main(String args[]) {
		System.out.println(roman2Decimal("IV"));
	}
}

- Enrique Correa August 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package Other;

import java.util.HashMap;

public class Roman {

	public static int roman2Decimal(String roman) {
		HashMap<Character, Integer> values = new HashMap<Character, Integer>();
		values.put('M', 1000);
		values.put('C', 100);
		values.put('L', 50);
		values.put('X', 10);
		values.put('V', 5);
		values.put('I', 1);

		int value = 0;
		for (int index = 0; index < roman.length();) {
			int tmp = 0;
			char current = roman.charAt(index);
			do {
				tmp += values.get(current);
				index++;
			} while( index + 1 < roman.length() && roman.charAt(index) == current);

			if (index < roman.length()
					&& values.get(current) < values.get(roman.charAt(index))) {
				tmp *= -1;
			}
			value += tmp;
		}
		return value;
	}

	public static void main(String args[]) {
		System.out.println(roman2Decimal("IV"));
	}
}

- Anonymous August 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class RomanNumeral {

	public static int getNumberr (char c) {
		switch(c) {
			case 'I':
				return 1;
			case 'V':
				return 5;
			case 'X':
				return 10;
			case 'L':
				return 50;
			case 'C':
				return 100;
			case 'D':
				return 500;
			case 'M':
				return 1000;
		}
		return 0;

	}
	public static int getNumber(String roman) {

		int x = 0;
		for (int i = 0; i < roman.length(); i++) {
			int a = getNumberr(roman.charAt(i));
			int b = (i+1) < roman.length() ? getNumberr(roman.charAt(i+1)) : 0;
			if (a < b) {
			 	x = x+ (b - a);
			 	i++;
			}
			else {
				x = x+ a;
			}
		}
		return x;

	}

	public static void main(String args[]) {
		String str = "MMMMCMXCIX";
		System.out.println(" value is "+getNumber(str));
	}

}

- Dalek October 03, 2015 | 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