Zillow Interview Question for Software Engineer / Developers


Country: United States




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

private long stringToLong(String pNumber) {
long result = 0;
String list = "0123456789";
for (int i = 0; i <= pNumber.length()-1; i++) {
char c = (char) pNumber.charAt(i);
int lastValue = list.indexOf(c);
result= result*10 + lastValue;
}
return result;
}

- Sourav July 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

isnt charAt() a built in java function????

- codingAddicted July 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the string must have converted to character array and this character array must have passed to the routine.

- cobra July 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

public static long StringToLong(String s) throws NumberFormatException{
	long n = 0;
	int factor = 1;
	int c;
	for(int i = s.length()-1; i > 0; --i ){
		c = s.charAt(i);
		if(c < 48 || c > 57){
			throw new NumberFormatException(s+" is not a valid number.");
		}
		n += (c-48) * factor;
		factor *= 10;
	}
	
	c = s.charAt(0);
	if(c == '-'){
		return -n;
	}else{
		n += (c-48) * factor;
	}
	return n;
}

- Sumit July 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your factor variable needs to be a long, otherwise this won't work with strings that are longer than an int can hold.

- WrongFactor October 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

long StringToLong(String str)
{
/* code goes here to convert a string to a long */ 
		long result = 0;
		int factor = 1;
		for(int i=str.length()-1;i>-1;i--)
		{
			char c = str.charAt(i);
			int k = c-48;
			result = (factor*k)+result;
			factor = factor * 10;
		}
		
		System.out.println("Therefore the long number is : " + result);
return result;
}

- Siri July 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static long stringToLong(String str){

long result = 0;
int factor =10;
char[] ch = str.toCharArray();
for(int i = 0;i<str.length();i++){
int temp = ch[i]-48;
if(temp>=0||temp<=9){
result = result*factor+temp;

}

}


return result;
}

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

public class stringtolong {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
long i = stringToLong("123");
if (i == 123)
System.out.println("success !!");
else
System.out.println("failure !!");
}

static long stringToLong(String s){
long value = 0, longNumber=1;
char counter; //counter to read each character in string
int i=s.length()-1;
while(i>=0)
{
counter = s.charAt(i);
value = value + (counter-48) * longNumber;
longNumber = longNumber * 10;
i--;
}
return value; //returns long value of the given string
}
}

- Mohit January 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string sx = "567";
int ix = 0;
for (int i = 0; i < sx.Length; i++)
{
ix *= 10;
ix += sx[i] - 48;
}
write ix;

- Kamaraj September 21, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Extarct all the characters from start and add it in long.

logic:

char[] lookupNumber = {'1','2','3'....'8','9'};
string c = "1000";

long l;
while(c!=empty)
{
extractedCharacter = extartFromStartOfString(string c);
l = l*10 + lookup(extractCharacter);
}

int lookup(char c)
{
return binarySearchInLookupNumber (C)
//this would return index.
}

This uses no inbuilt function of any language.

- nerd July 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"binarySearchInLookupNumber" -> binary search in an array of 10 characters? sounds extreme to me!

- Anonymous July 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

int strlen(const std::string a){
int length=0;
while(a[length])
length++;
//throw exception if string is empty
if(length<1) throw -1;
return length;
}
//look up the ascii code
//and check if any non-numerical char exist
long lookup(char a){
int temp=int(a);
if(temp<48 || temp>57) throw temp;

switch(temp){
case 48:
return 0;
case 49:
return 1;
case 50:
return 2;
case 51:
return 3;
case 52:
return 4;
case 53:
return 5;
case 54:
return 6;
case 55:
return 7;
case 56:
return 8;
case 57:
return 9;
default:
return -1;
:#include "atol.h"

//compute string length
int strlen(const std::string a){
int length=0;
while(a[length])
length++;
//throw exception if string is empty
if(length<1) throw -1;
return length;
}
//look up the ascii code
//and check if any non-numerical char exist
long lookup(char a){
int temp=int(a);
if(temp<48 || temp>57) throw temp;

switch(temp){
case 48:
return 0;
case 49:
return 1;
case 50:
return 2;
case 51:
return 3;
case 52:
return 4;
case 53:
return 5;
case 54:
return 6;
case 55:
return 7;
case 56:
return 8;
case 57:
return 9;
default:
return -1;
}
}

//convert string to long
long stringToLong(const std::string s){
int len=strlen(s);
long temp=0;
long temp2=0;
//flag for negative input
int flag=1;
int i=0;

//check negativity
if(s[0]=='-'){
flag=-1;
i++;
}

for(;i<len;i++)
{
//throw an exception if the number is too large (or too small)
if((LONG_MAX-10*temp)<lookup(s[i])) throw 99;
temp=10*temp+lookup(s[i]);
}
return flag*temp;
}

- pp November 18, 2013 | 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