OnMobile Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Written Test




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

result = greater number - smaller number

In the result check if there are consecutive zeros of length equal to the length of smaller number(from right) , if so return the index where the last zero is present in the result

- Rajesh January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is wrong approach. Consider 654455 and 44

- Anonymous June 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Obviously cannot use library functions.

- sjosh.theonlyone January 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In C++ :

int lengthOfNumber (int num) {
	
	int temp, i = 0;
	temp = num;
	
	while (temp > 0) {
		temp = temp/10;
		i++;
	}
	
	return i;
}

int returnIndex(int a, int b)  {
	
	int length_a = lengthOfNumber(a);
	int length_b = lengthOfNumber(b);
	int divisor = 10^length_a;
	int counter = 0;
	int rem = b, div = b;

	while (div != 0) {
		rem = rem%divisor;
		if (rem == a)
			return length_b - counter - length_a;
		div = div/10;
		counter++;
	}
	
	return -1; //a does not exist in b
}

- Maneet & Arjun January 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In my opinion, you should make rem = div%divisior;

- Manny Lee January 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

divisor in while is constant
so in first itr
rem = 45
second
0
third 0
and so on.....

I am missing something here...?I didnt try it with a compiler..

PS: apologies for reposting....I dnt know what I am doing...time for coffee..

- sjosh.theonlyone January 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <stdio.h>

using namespace std;

int main() {

    int numa = 126547;
    int numb = 654;
    int temp = numb;
    int index = 0;
    int traversal = 0;


    if (numa < numb) {
        temp = numa;
        numa = numb;
        numb = temp;
        temp = numb;
    }

    while (numa > 0) {
        if (temp > 0) {
            if (numa % 10 == temp % 10) {
                temp = temp / 10;
                if (temp == 0)
                    index = traversal;
            } else {
                temp = numb;
            }
        }
        numa = numa / 10;
        traversal++;
    }

    if (temp > 0)
        cout << " NOt found ";
    else
        cout << " Found at " << traversal - index;
}

- isandesh7 January 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In JAVA :

public class Test1 {
    
    public static void main(String args[]) {
        
        int a = 12345;
        String sa = Integer.toString(a);
        int saLen = sa.length();
        
        int b = 34;
        String sb = Integer.toString(b);
        int sbLen = sb.length();
        
        int match;
        
        for( int i = 0 ; i < saLen ; i++ ) {
            
            match = 1;
            
            if( sa.charAt(i) == sb.charAt(0) ) {
            
                 for( int j = 0 ; j < sbLen ; j++ ) {
                     if( sa.charAt(i+j) != sb.charAt(j) ) {
                         match = 0;
                         break;
                     }
                 }
                 
                 if( match == 1 ) {
                     System.out.println(" Index Found at i : " + i);
                     System.exit(0);
                 }
            
            }
            
        }
        
    }
    
}

- Another Coder January 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There is an algorithm for pattern matching, KMP algorithm which solves the problem in O(n) time

- Anonymous January 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

put the digits of num1 and num2 in 2 arrays number and pattern respectively. Apply KMP algorithm now. This will be O(m+n) time.

- Anonymous January 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int NumberInNumber(int n1, int n2)
{
  int temp1 = n1;
  int index = 0;
  int temp2 = n2;
  
  while(temp1)
    {
      while(temp1%10 == temp2%10 && temp2)
	{
	  temp1/=10;
	  temp2/=10;
	}
      if(!temp2)
	{
	while(temp1)
	  {
	    index++;
	    temp1/=10;
	  }
	return index;
	}
      temp1/=10;
      temp2=n2;
    }
  
  return -1;
}

- Anonymous January 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int intIndex(int a, int b)
{
   // assume a and b are positive, otherwise use abs
   int power = 1;
   int a_len = 1, b_len = 1;
   while (power < a)
   {
      a_len++;
      power *= 10;
   }

   int index = -1;
   for (int i = 0; b > 0; i++, b /= 10)
   {
      if (b % power == a)
      {
         index = i;
      }
      b_len++;
   }

   return (index > 0) ? (b_len - index - a_len) : -1;
}

- Anonymous January 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
#include <stdio.h> #if 0 /* logic here */ pattern: 12345 find: 23 count = 2 /* 23 has two integers */ so divide with 10*10 first iteration: 12345%100 = 45 second iteration: 1234%100 = 34 third ..... : 123%100 = 23 /* so found here */ #endif int main() {{{ int pattern = 456789101; int find = 10; int count = 0; int i, flag, divide_with = 1; /* count the integers in the find */ i = find; do {{{ i = i/10; count++; }}}while(i); /* get the 'count' number of integers from pattern */ i = pattern; while(count) {{{ divide_with = divide_with*10; count--; }}} flag = 0; do {{{ i = pattern%divide_with; pattern = pattern/10; if(i == find) {{{ flag = 1; break; }}} count++; }}}while(pattern); if(flag) printf("found\n"); else printf("not found\n"); return 0; }}} - anish[1] January 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thank you all. I did something similar. Cheers. :)

- sjosh.theonlyone January 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just correct the first answer.
BTW, the "^" symbol is not power in c++. It's bitwise XOR.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int length (int num) {

int temp, i = 0;
temp = num;

while (temp > 0) {
temp = temp/10;
i++;
}

return i;
}

int index(int a, int b) {

int len_a = length(a);
int len_b = length(b);
int divisor = static_cast<int>(pow(10,length_a));
int counter = 0;
int rem = b, div = b;

while (div != 0) {
int index = rem%divisor;
rem = rem/10;
if (index == a)
return len_b - counter - len_a;
div = div/10;
counter++;
}

return -1; //a does not exist in b
}

int main()
{
int a = 34, b = 12345;
int index = index(a, b);
printf("The index is: %d\n", index);
return 0;
}

- Anonymous January 22, 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