Twitter Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

public class RomanNumeral {

    public int getDecimal(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;
            default: return 0;
        }
    }

    public int convert(String roman) {
        char[] n = roman.toCharArray();
        int total = 0;
        char previous = n[n.length-1];
        for (int i = n.length-1; i>=0; i--) {
            int p = getDecimal(previous);
            int c = getDecimal(n[i]);
            if (p > c) total-=c;
            else {
                total+=c;
                previous = n[i];
            }
        }
        return total;
    }

    public static void main(String args[]) {
        RomanNumeral rn = new RomanNumeral();
        System.out.println(rn.convert("XXIV"));
    }
}

- george.maggessy April 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int romanNumber(String str)
    {
	int sum=0;
	int prev=check(str.charAt(0));
	
	for (int i =1;i<str.length();i++)
	{
	    int current = check(str.charAt(i));
	    
	    if (current > prev)
	    {
		sum -=prev; 
	    }
	    else
	    {
		sum += prev;
	    }
	    
	    prev = current;
	}
	
	sum +=prev;
	return sum;
    }

- Anonymous June 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<string>

using namespace std;

int roman_number(string str);

int main() {
        char s[80];
        int value;
        scanf("%s",s);
        value = roman_number(s);
        printf("The roman value is %d",value);
        return 0;
}

int roman_number(string str)
{
        bool flagI = false, flagX = false, flagC = false;
        int cnt[] = {0,0,0,0,0,0,0};
        int i, sum=0, len = str.length();
        char ch;
        for(i = len-1;i >= 0; i--) {
                ch=str.at(i);
                switch(ch) {
                        case 'I':
                                cnt[0]++;
                                if ((flagX || flagC) ||
                                   (cnt[0]>3) || (flagI && cnt[0]>1)) {
                                        printf("wrong input \n");
                                        goto err;
                                }
                                if (!flagI) {
                                        sum = sum + 1;
                                } else {
                                        sum = sum - 1;
                                }
                                break;
                        case 'V':
                                cnt[1]++;
                                if (cnt[1]>1) {
                                        printf("wrong input \n");
                                        goto err;
                                }
                                flagI = true;
                                sum = sum + 5;
                                break;
                        case 'X':
                                cnt[2]++;
                                if (flagC || (cnt[2]>3) || (flagX && cnt[2]>1)) {
                                        printf("wrong input \n");
                                        goto err;
                                }
                                flagI = true;
                                if (!flagX) {
                                        sum = sum + 10;
                                } else {
                                        sum = sum - 10;
                                }
                                break;
                        case 'L':
                                cnt[3]++;
                                if (cnt[3]>1) {
                                        printf("wrong input \n");
                                        goto err;
                                }
                                flagX = true;
                                sum = sum + 50;
                                break;
                        case 'C':
                                cnt[4]++;
                                if (cnt[4]>3 || (flagC && cnt[4]>1)) {
                                        printf("wrong input \n");
                                        goto err;
                                }
                                flagX = true;
                                if (!flagC) {
                                        sum = sum + 100;
                                } else {
                                        sum = sum - 100;
                                }
                                break;
                        case 'D':
                                cnt[5]++;
                                if (cnt[5]>1) {
                                        printf("wrong input \n");
                                        goto err;
                                }
                                flagC = true;
                                sum = sum + 500;
                                break;
                        case 'M':
                                cnt[6]++;
                                if (cnt[6]>3) {
                                        printf("wrong input \n");
                                        goto err;
                                }
                                flagC = true;
                                sum = sum + 1000;
                                break;
                }
        }
        return sum;
        err:
        return -1;
}

- Anonymous October 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

static {

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

}

public int romanToInt(String s) {
int intNum = 0;
int prev = 0;
for (int i = s.length() - 1; i >= 0; i--) {
int temp = romanMap.get(s.charAt(i));
if (temp < prev)
intNum -= temp;
else
intNum += temp;
prev = temp;
}
return intNum;
}

- nikitakothari21 December 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

int keyval[] = { 1000, 900, 500, 400, 100,90, 50, 40, 10, 9, 5, 4, 1 };
char keystr[13][] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
int i;
char str[] = "XXIV";
char *ptr=str;
int val=0;
for(i=0; i<13 ; i++)
{
  l=strlen(keystr[i]);
  if(!strncmp(ptr,keystr[i],l))
  {
    val+=keyval[i];
    ptr+=l;
  }
}
printf("\n val: %d", val);

- CheckThisResume.com March 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your code is both inefficient and incorrect. For starters try and understand the problem, take a few examples and then write the code. You calculate string length for all possible combinations each time. Whereas this problem can be solved using one pass linear scan.

- ashish.kaila March 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is one pass scan Sir!
if the strlen bothers you, replace 'l' with ls[i] with ls defined as follow: int ls[]={ 1, 2, 1, 2, etc...

- CheckThisResume.com March 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I replied above for the "inefficient". Let me reply for the "incorrect". I made a small change with "while" instead of "if" and tested it OK.

#include <stdio.h>

int keyval[] = { 1000, 900, 500, 400, 100,90, 50, 40, 10, 9, 5, 4, 1 };
char keystr[13][3] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
char str[] = "XXIV";

void decode()
{
  char *ptr=str;
  int i, l;
  int val=0;
  for(i=0; i<13 ; i++)
  {
    l=strlen(keystr[i]);
    while(!strncmp(ptr,keystr[i],l))
    {
      val+=keyval[i];
      ptr+=l;
    }
  }
  printf("\n val: %d", val);
}

void main()
{
  int i;
  decode();
  exit(000);
}

- CheckThisResume.com March 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Let me give you a hint. You can solve this by scanning each character once and you don't need to keep compound character sets like IV, IX etc. Think about addition and subtraction. You are thinking too complex.

- ashish.kaila March 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

There is no error checking and it's way to inefficient. You can improve by using a hash instead and going one character at a time.

- Anonymous May 13, 2012 | Flag


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