Interview Question for Software Engineer / Developers






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

boolean isArray(vector<int> A, int num) {
    int n=A.size();
    int pos=0,diff;
    while (pos<n) {
        diff=abs(num-A[pos]);
        if (diff==0) return true;
        pos+=diff;
    }
    return false;
}

- supersonglj August 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

boolean inArray(int[] A, int num) {
return A[0]<=num && num<=A[A.len-1];
}

- Hendry August 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ignore my previous solution, it is incorrect.

- Hendry August 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

don't say such silly things

- Anonymous August 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

check min<=num && num<=max ? But it requires go through each element...
eg:
1,2,3,4,5,6
2,1,2,3,2,3
4,5,4,3,2,3

- Anonymous August 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A better solution. The complexity of best case is O(1); worst case is O(n).

boolean isArray(int[] A, int num)
{
int len = A.len;
int m = len - 1 - abs( A[0] - A[len-1]);
if (A[0]<=num && num<=A[len-1])
return true;
else if (num>A[0]+m || num<A[0]-m)
return false;
else
// go through each element
}

- Hendry August 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

boolean isArray(int[] A, int num)
{
int len = A.len;
int m = (len - 1 - abs( A[0] - A[len-1]) ) / 2;
if (A[0]<=num && num<=A[len-1])
return true;
else if (num>A[0]+m || num<A[0]-m)
return false;
else
// go through each element
}

- Hendry August 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The number is in array'A' if:
1. 0 <= num-a[0] < n when a[0] < a[n-1]...consecutive ascending numbers
2. 0 <= -(num-a[0]) < n when a[0] > a[n-1]...consecutive descending numbers

- ABC August 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry...ignore the above solution...didn't consider the fact that next element might be +/- 1...

- ABC August 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

A = [8, 9, 10, 11, 10, 9, 8, 7, 6, 5, 6]

according to your algorithm, it is in case 2 since A[0] > A[n-1]
if num = 4, 0 <= - (4 - 8) < 11, returns true. But 4 is not in the array.

- Hendry August 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Seems related to One-dimensional random walk.
en.wikipedia.org/wiki/Random_walk#One-dimensional_random_walk

- Guest123 August 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Hendry can you please explain your solution

- testing August 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Hendry your code fails for

int num=2;
int arr[]={3,2,1};

- testing August 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int find_index(int *a, int n, int x) {
int i = 0;
while(i < n && a[i] != x)
i += abs(a[i] - x);
return i < n ? i : -1;
}

- Learner September 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@learner your code will fail for -4,-3,3,4 and element to be search is 3

- testing September 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

As,the distance between adjacent items is either +1 or -1, we just have to move forward the relative distance between search num and current element.

- Learner September 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

ur input is incorrect:
-4, -3, ........, 3, 4:-> Every adjacent element should have distance of either +1 or -1
. On the contrary -3....3 has a distance of 6>
Run my also on this input:
-4, -3, -2, -1, 0, 1, 2 , 3 , 4

- @tester September 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@learner
I think your code will fail for this example:
-3 -2 -1 0 -1 0 1 2 and the element to be searched is 2

- Spb September 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@learner can you explain your thought process/algorithm

- testing September 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

As the distance between every adjacent element is either +1 or -1, we just have to move the relative distance between the search element and the current element.

- Learner September 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Random;

public class FindInteger {

public static int findInteger(int k, int[] a, int start, int end) {

if (start < end) {
if (a[start] <= a[end]) {
// v is the numbers of decreases
int minus = (end - start - (a[end] - a[start])) / 2;
int plus = minus + (a[end] - a[start]);

if (k >= Math.max(a[start] - minus, a[end] - plus)) {
if (k <= Math.min(a[end] + minus, a[start] + plus)) {
// integer could validated
if (k == a[start])
return start;
else if (k == a[end])
return end;
else {
int t = findInteger(k, a, start, (start + end) / 2);
return t == -1 ? findInteger(k, a,
(start + end) / 2 + 1, end) : t;
}
} else {
return -1;
}
} else {
return -1;
}
} else {
int plus = (end - start - (a[start] - a[end])) / 2;
int minus = plus + (a[start] - a[end]);

if (k >= Math.max(a[end] - plus, a[start] - minus)) {
if (k <= Math.min(a[start] + plus, a[end] + minus)) {
if (k == a[start]) {
return start;
} else if (k == a[end]) {
return end;
} else {
int t = findInteger(k, a, start, (start + end) / 2);
return t == -1 ? findInteger(k, a,
(start + end) / 2 + 1, end) : t;
}
} else {
return -1;
}
} else {
return -1;
}
}
} else {
return a[start] == k ? start : -1;
}

}

public static void main(String[] args) {
Random random = new Random();

int k = 50;

int[] a = new int[10000];
a[0] = 5;
for (int i = 1; i < a.length; i++) {
int t = Math.abs(random.nextInt()) % 3;
if (t == 2) {
a[i] = a[i - 1] - 1;
} else {
a[i] = a[i - 1] + 1;
}
if (a[i] == k) {
System.out.println("Insert at #" + i);
}
}

int i = findInteger(k, a, 0, a.length - 1);
System.out.println("Found at #" + i);
}
}

- Zoxi September 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

@Spb: i don't understand how u deduce the same. Check my algo step by step below:
1. i = i + abs(-3 - 2); //i = 5
2. i = i + abs(0-2); // i = 7
3. a[7] = 2 //Find the element

- Learner September 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Learner
yes you are right...sorry...i was very tired....Thanks for the reply

- Spb September 02, 2011 | Flag


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