ThoughtWorks Interview Question for Software Engineers


Country: United States
Interview Type: Phone Interview




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

Simple program to convert roman to decimal number:

#include <stdio.h>
#include <string.h>


int main() {
	char input[100];
	printf("\nEnter a number with roman numerals [Please ener valid number as not checking validity]: ");
	scanf("%s", input);
	
	int output;
	int i = 0;
	int len = strlen(input);
	while(i < len){
		char ch = input[i];
		switch(ch) {
			case 'M':
				output += 1000;
				break;
			case 'D':
				output += 500;
				break;
			case 'C':
				//It can be taken as +100 or -100 
				//It will be 100 if input after this is D or M
				//else 
				//-100
				if(i+1 <= len && (input[i+1] == 'D' || input[i+1] == 'M'))
					output += -100;
				else 
					output += 100;
				break;
			case 'L':
				output += 50;
				break;
			case 'X':
				//It can be taken as +10 or -10 
				//It will be 10 if input after this is L or C
				//else 
				//-10
				if(i+1 <= len && (input[i+1] == 'L' || input[i+1] == 'C'))
					output += -10;
				else 
					output += 10;
				break;
			case 'V':
				output += 5;
				break;
			case 'I':
				//It can be taken as +1 or -1 
				//It will be 1 if input after this is V or X
				//else 
				//-1
				if(i+1 <= len && (input[i+1] == 'V' || input[i+1] == 'X'))
					output += -1;
				else 
					output += 1;
				break;
				
		}
		i++;
	}
	
	printf("\nGiven number in decimal is : %d", output);
}

- ashu1.220791 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