Facebook Interview Question for Software Engineer Interns


Country: United States
Interview Type: Phone Interview




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

public class AtoF {
	String str;
	float result;
	
	public AtoF(String s) throws Exception{
		this.str = s.trim();
		result = solve(str);
	}
	
	public float solve(String str) throws Exception{
		int indexOfE = str.indexOf('E');
		if(indexOfE == -1)
			indexOfE= str.indexOf('e');
		if(indexOfE == -1){
			return withoutE(str);
		}else{
			return (float) (withoutE(str.substring(0, indexOfE))*
					Math.pow(10, withoutE(str.substring(indexOfE+1))));
		}
	}
	

	private float withoutE(String s) throws Exception{
		if(s.charAt(0) == '-'){
			return -withoutE(s.substring(1));
		}
		int indexOfPeriod = s.indexOf('.');
		if(indexOfPeriod == -1){
			return makeInt(s);
		}else{
			float beforePeriod = makeInt(s.substring(0,indexOfPeriod));
			float afterPeriod = makeInt(s.substring(indexOfPeriod+1));
			float temp = afterPeriod;
			int countDigit = 0;
			while(temp > 10){
				countDigit ++;
				temp /=10;
			}
			countDigit++;
			
			return (float)(beforePeriod + Math.pow(10, -countDigit)*afterPeriod);
		}
		
	}
	

	private float makeInt(String s) throws Exception{
		if(s.length() == 0)
			return 0;
		float res = 0f;
		for (int i = 0; i < s.length(); i++) {
			// for example 234 = ((2*10)+3)*10+4;
			res = res*10 + makeDigit(s.charAt(i));
		}
		return res;
	}
	
	
	private float makeDigit(char charAt) throws Exception {
		switch(charAt){
		case '0':
			return 0;
		case '1':
			return 1;
		case '2':
			return 2;
		case '3':
			return 3;
		case '4':
			return 4;
		case '5':
			return 5;
		case '6':
			return 6;
		case '7':
			return 7;
		case '8':
			return 8;
		case '9':
			return 9;
		default:
			throw new Exception();
				
		}
	}

	public static void main(String[] args) throws Exception {
		AtoF a = new AtoF("3.2");
		System.out.println(a.result);
	}
}

- mahdi.oraei February 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Reads from arguments:

public class Convert {
    public static void main(String[] argv) {
	String num = argv[0];
	// split string is not a parsing function.
	// you could write a for loop instead if you like
	String[] num_pow = num.split("E");
	if (num_pow.length == 1) {
	    num_pow = num.split("e");
	}
	double f = 0.0;
	int p = 0;
	char[] dec = num_pow[0].toCharArray();
	while(dec[p++] != '.');
	p--;
	double c = Math.pow(10.0, p - 1);
	for (int k = 0; k < dec.length; k++) {
	    if (dec[k] != '.') {
		f = f + c * floatOf(dec[k]);
		c = c / 10.0;
	    }
	}
	// Decode the exp. term
	char[] exp_char = null;
	boolean neg = false;
	if (num_pow[1].charAt(0) == '-') {
	    exp_char = num_pow[1].substring(1).toCharArray();
	    neg = !neg;
	}
	else if (num_pow[1].charAt(0) == '+') {
	    exp_char = num_pow[1].substring(1).toCharArray();
	}
	else {
	    exp_char = num_pow[1].toCharArray();
	}    

	double e = 0.0;
	c = Math.pow(10, exp_char.length - 1);
	p = 0;
	while(p < exp_char.length) {
	    e += c * floatOf(exp_char[p]);
	    c /= 10;
	    p++;
	}
	if (neg) {
	    e = -e;
	}
	f *= Math.pow(10.0, e);
	System.out.println(f);
    }

    public static double floatOf(char dig) {
	    switch(dig) {
	    case '0':
		return 0.0;
	    case '1':
		return 1.0;
	    case '2':
		return 2.0;
	    case '3':
		return 3.0;
	    case '4':
		return 4.0;
	    case '5':
		return 5.0;
	    case '6':
		return 6.0;
	    case '7':
		return 7.0;
	    case '8':
		return 8.0;
	    case '9':
		return 9.0;
       }
	    return -1;
    }


}

- Ehsan February 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hi, here a c++ solution...im not sure what you mean by built-in in function...

#include <iostream>
#include <cmath>
#include <sstream>
using namespace std;

float string_to_float(const string &number) {
	int exp_pos = number.find("E");
	int exp_num = 0;
	float float_num = 0.0;
	
	if(exp_pos != string::npos) {
		stringstream ss;
		ss << number.substr(exp_pos + 1, number.size() - exp_pos - 1);
		ss >> exp_num;
	}
	else {
		return float_num;
	}
	
	stringstream ss;
	ss << number.substr(0, exp_pos);
	ss >> float_num;
	
	float_num = float_num * pow(10, exp_num);
	
	return float_num;
}

int main() {
	// your code goes here
	
	cout << string_to_float("342.18E-2") << endl;

	return 0;
}

- Gerald February 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int inv(char c){
return (int)c - int('0');
}

int range(int c){
return c >= 0 && c <= 9 ? 1 : 0;
}
void convert(){

char *A = "123.145E-3";
int a = strlen(A);
int power = 1;
int rest = 0;
int expvalue = 0;
double restvalue = 0;
int j = 0;
int decimal1 = 0;
int decimal = 0;
for (int i = 0; i < a; i++){
if (rest){
if (range(inv(A[a - i - 1]))){
restvalue = restvalue + inv(A[a - i - 1])*pow(10, j);
j++;
}
else if (A[a - i - 1] == '.'){
//expvalue *= -1;
//power = 0;
//j--;
decimal = i - decimal1-1;
//cout << decimal;
}

}
if (power){
if (range(inv(A[a - i - 1]))){
expvalue = expvalue + inv(A[a - i - 1])*pow(10, i);
}
else if (A[a - i - 1] == '-'){
expvalue *= -1;
power = 0;
}
else if (A[a - i - 1] == 'E'){
rest = 1;
j = 0;
decimal1 = i;
power = 0;
}
//(A[a-1-i])
}
else if (A[a - i - 1]=='E'){
rest = 1;
j = 0;
decimal1 = i;
power = 0;
}


}
restvalue = restvalue*pow(10, -decimal)*pow(10, expvalue);
cout << restvalue;
};

- Anonymous June 17, 2014 | 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