Apple Interview Question for Software Engineer / Developers






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

Apple asked me this during a phone interview last year. I offered them something similar.

/* assuming the string has no non-digit characters */
int atoi(char *s)
{
    int result = 0;

    while (*s)
        result = 10*result + *s++ - '0';

    return result;
}

- no-one March 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using sscanf be sufficient??

- Anonymous March 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Correct solution.

Though I believe the above solution is recommended, still thought to share its recursive version.

Things need to be considered in this problem
1. Checking valid input.
2. Checking overflow.
3. Minimum memory allocation for local variables.
4. More usage of bitwise than arithmetic operators.

int atoi(char *inputParam){
    int result = 0;
    static int factor = 1;
    if(*inputParam == 0) return 0; // Terminating condition
    if( (*inputParam <'0') || (*inputParam > '9'))  // Validation
        throw new ArgumentException("Given string cannot be parsed into integer");
    // Recurring code snippet.
    int intermediateResult = atoi(inputParam + 1);
    result = (*inputParam & 0xF) * factor * intermediateResult;
    factor *=10;
    return result;
}

Can someone help me understanding the overflow checking ?

- ankushbindlish May 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You should test your code before pasting here.

- Anon November 10, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int atoi(string s)
{

//Handles the null and empty string case
if(string.IsNullOrEmpty(s))
throw new NullReferenceException();

int i = 0;
bool isNeg = false;
int num = 0;

//check if it's a negative and move the index
if (s[i] == '-')
{
isNeg = true;
i++;
}

while(i < s.Length)
{
num = num *10;
num = num + (s[i] - '0');
i++;
}

if (isNeg)
num = num * -1;

return num;
}

- Anonymous May 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static int  do_itoa(String str){
			int p=1;
			int res=0;
			int len= str.length();
			
			
			for(int i=len-1 ; i >= 0 ; i--){
				int temp = str.charAt(i) -'0';
				if(temp < 0  || temp > 9){
					res = -1;
					break; //Stuff other than numbers are not allowed
				}
				res += p* (temp);
				p*=10;
			}
			return res;
	}

- Vinoth June 18, 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