Interview Question for Software Engineer / Developers






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

<pre lang="java" line="1" title="CodeMonkey20076" class="run-this">class Caller {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Caller c=new Caller();
System.out.print(c.convertToRoman(1681));
}
class Romans{
int deciValue;
char value;
Romans(int deciValue, char value){
this.deciValue=deciValue;
this.value=value;
}
}
public String convertToRoman(int num)
{
StringBuffer sb=new StringBuffer();
Romans[] romans = getRomans();
for(int i=0;i<romans.length;i++)
{
int count=num/romans[i].deciValue;
num=num-(romans[i].deciValue*count);
while(count !=0){
sb.append(romans[i].value);
count--;
}
}
return sb.toString();
}
private Romans[] getRomans() {
Romans roman1=new Romans(1000,'M');
Romans roman2=new Romans(500,'D');
Romans roman3=new Romans(100,'C');
Romans roman4=new Romans(50,'L');
Romans roman5=new Romans(10,'X');
Romans roman6=new Romans(5,'V');
Romans roman7=new Romans(1,'I');
Romans[] romans=new Romans[]{roman1,roman2,roman3,roman4,roman5,roman6,roman7};
return romans;
}

}
</pre><pre title="CodeMonkey20076" input="yes">1681
</pre>

- prolific.coder August 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static string IntegerToRoman(int number)
        {
            // Validate
            if (number < 0 || number > 3999)
            {
                throw new ArgumentException("Value must be in the range 0 - 3,999.");
            }

            if (number == 0) return "N";

            // Set up key numerals and numeral pairs
            int[] values = new int[] { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
            string[] numerals = new string[] { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };

            StringBuilder result = new StringBuilder();

            // Loop through each of the values to diminish the number
            for (int i = 0; i < 13; i++)
            {
                // If the number being converted is less than the test value, append
                // the corresponding numeral or numeral pair to the resultant string
                while (number >= values[i])
                {
                    number -= values[i];
                    result.Append(numerals[i]);
                }
            }
            // Done
            return result.ToString();
        }

- manish August 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

We can't store bunch of mappings for each digit i.e 9, 60,4 are all superfluous. If you want to argue for it, I can argue that I'd store all the mappings from 1-5000 in an array, precompute them and return values in constant time.

- prolific.coder August 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

static string intToRoman(int num) 
        {  
            string roman = "";
            int h, t, o, th;
            string[] ones = {"","I","II","III","IV","V","VI","VII","VIII","IX"};
            string[] tens = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" };
            string[] hundreds = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
            string[] thousands = { "", "M", "MM", "MMM", "MMMM", "MMMMM" };   
            if (num <= 5000)
            {    
                th = num / 1000;
                num = num % 1000;
                h = num / 100;
                num = num % 100;
                t = num / 10;    
                o = num % 10;  
  
                roman = roman + thousands[th] + hundreds[h] + tens[t] + ones[o];  
            } 
            else 
            {
                roman = "Please enter a smaller number\n"; 
            }
        
            return roman; 
        }

- another solution August 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here's what I got in my first attempt, although I like manish's solution better -

public static String getRoman(int arabicNum) {
		StringBuilder roman = new StringBuilder();
		if(arabicNum < 1 || arabicNum > 3999) return roman.toString();
		
		if (arabicNum /1000 > 0) {
			int mCount = arabicNum/1000;
			arabicNum = arabicNum % 1000;
			for(int i = 0 ; i < mCount; ++i) {
				roman.append('M');
			}
			
		}
		
		if(arabicNum > 899) {
			roman.append("CM");
			arabicNum = arabicNum % 900;
		}
		
		
		if(arabicNum > 500) {
			roman.append("D");
			arabicNum = arabicNum % 500;
		}
		
		if(arabicNum > 399) {
			roman.append("CD");
			arabicNum = arabicNum % 400;
		}
		
		if(arabicNum > 99) {
			int numC = arabicNum / 100;
			for(int i = 0; i < numC; ++i) {
				roman.append('C');
			}
			arabicNum = arabicNum % 100;
		}
		
		if(arabicNum >89) {
			roman.append("XC");
			arabicNum = arabicNum % 90;
		}
		
		if(arabicNum > 49) {
			roman.append('L');
			arabicNum = arabicNum % 50;
		}
		
		if(arabicNum > 39) {
			roman.append("XL");
			arabicNum = arabicNum % 40;
		}
		
		if(arabicNum > 9) {
			int numX = arabicNum / 10;
			for(int i = 0; i < numX; ++i ) {
				roman.append('X');
			}
			arabicNum %= 10;
		}
		
		if (arabicNum > 8) {
			roman.append("IX");
			arabicNum %= 9;
		}
		
		if(arabicNum > 4) {
			roman.append('V');
			arabicNum %= 5;
		}
		
		if(arabicNum > 3) {
			roman.append("IV");
			arabicNum %= 4;
		}
		
		if(arabicNum > 0) {
			for(int i = 0; i < arabicNum; ++i) {
				roman.append('I');
			}
		}
		return roman.toString();
	}

- Anonymous August 09, 2010 | 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