Yahoo Interview Question for Software Engineer / Developers






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

//Just like our decimal system with 26 instead of 10

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

// Utility Function
char* reverseString(char* str)
{
	int start = 0;
	int end = getLength(str) - 1;
	
	while(start < end)
	{
		char ch = str[end];
		str[end] = str[start];
		str[start] = ch;
		
		start++; end--;
	}
	return str;
}

// Utility Functions
int getLength(char* str)
{
	int i = 0;
	while(*str)
	{
		i++;
		str++;
	}
	return i;
}
//Works only for lower case characters a-z
int getEquivalentNumber(char* input)
{
	int code = 0;
	int length = getLength(input);
	--length;
	
	while(length >= 0)
	{
		int value = (*input) - 96;
		code = code + value*(pow(26,length));
		
		length--;
		input++;
	}
	return code;
}

//Works only for lower case characters a-z
char* getEquivalentCode(int input)
{
	char* result = (char*)malloc(10*sizeof(char));
	char* tempResult = result;
	
	int alphabet= 26;
	
	while(input)
	{
		int ch = input % alphabet;
		input = (input - ch) / alphabet;
		
		*result = (ch + 96);
		result++;
	}	
	*result = '\0';
	return reverseString(tempResult);
}

int main()
{
	char input[5] = "kds"; 
	int result = getEquivalentNumber(input);
	printf("%s equivalent to %d\n", input,result);
	
	int _input = 7556;
	char* _result = getEquivalentCode(_input);
	printf("%d equivalent to %s", _input,_result);
}

- Mahesh September 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string numToStr(int n) {
string str(1, 'a' + n%26);
n = n/26;
while (n != 0) {
str = (char)('a' + (n-1)%26) + str;
n = (n-1)/26;
}
return str;
}

- rajiv March 14, 2011 | 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