Interview Question for Web Developers


Country: United States
Interview Type: Phone Interview




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

I have been trying to find a solution to improve the cost but I haven't been able to. Solution with O(n^2) time and O(n) memory

def thereIsSum(seq):
     valid_sums = set(seq)
     for x in xrange(len(seq)):
         for y in xrange(x+1, len(seq)):
             if seq[x] + seq[y] in valid_sums:
                 return True
     return False

- Fernando August 01, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

input array has only 3 elements or more.

- lparka123 July 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ChrisK I thought about that but I got stuck, I can see a linear algorithm using two counters if you have a number to guide the search. I mean given the next array

[10, 20, 35, 40, 75, 90, 180]

If you have two counters one for the lower element and one for the higher element how you do modify them? Potentially any number except the two first ones and the last one can be used to generate a solution.
I have been thinking about some other approaches but none worked.

- Fernando August 01, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ChrisK
-- how about -ve numbers ?

arr = [-2,-1,0,1, 3 ]
x = sum_in_array_sort(arr,-1)
print (x)

- NoOne August 01, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ChrisK that is the problem I was encountering if you have the desired sum you can guide the search using two counters but on the given problem you have length of the array - 2 possible desired sums (the two first elements of the array can't be the desired sum for obvious reasons) so you can't use the algorithm as is to solve the problem.

- Fernando August 01, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ArraySum {

public static void main(String[] args) {
checksuma ch = new checksuma();
int as[] = { 20, 40 ,20};
System.out.println(ch.methsummc(as));
;
}

}

class checksuma {
boolean b = false;

public boolean methsummc(int ex[]) {
for (int i = 0; i < ex.length; i++) {
for (int j = 0; j < ex.length; j++) {
for (int k = j + 1; k < ex.length; k++) {
if (ex[i] == ex[j] + ex[k]) {
b = true;
}
}

}

}
return b;
}

}

- sudheer renu August 02, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Fernando, now I see, the sum is any number in the array. It seems like 3-sum and there are better solutions far beyond simple. For your solution, be careful to not reuse elements in the sum.

- Chris August 02, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool is_sum_of_two_elem_present(vector<int>&& arr) {
  sort(arr.begin(), arr.end());
  for (auto target = arr.rbegin(); target != next(arr.rend(), -2); ++target) {
    auto to_find = *target - *next(target);
    if (binary_search(next(target,2), arr.rend(), to_find, std::greater<int>())) return true;
  }
  return false;

- speedster August 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function check(arr){
  
  var a = arr.sort()
 console.log(a) 
  for(var i = 0; i < a.length -2; i++){
    var t = ( Math.abs(a[i+2]) == Math.abs(a[i]) + Math.abs(a[i+1]) )
    if(t){
      return true
    }
  }
  return false;
  
}

console.log(check([1,2,3])) // true
console.log(check([1,7,6])) // true
console.log(check([-1,9,8])) // true
console.log(check([-1,4,8])) // false
console.log(check([-3,-5,-8])) // true

- Siddharth January 15, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

x = [10,20,30];

for(var i= 0; i < x.length; i++)
{
a =0;
b=0;
c= 0;
if(i < x.length){
a=i;
}
else{
a=0;
}
if(a < x.length-1){
b=a+1;
}
else{
b=0;
}
if(b < x.length-1){
c=b+1;
}
else{
c=0;
}
if(x[a] == x[b] + x[c]) {
console.log("true" + " " + ":" + x[a] + "=" + x[b] + "+" + x[c]);
}
else{
console.log("false" + " " + ":" + x[a] + "=" + x[b] + "+" + x[c]);
}

}

Try copy pasting in console and this will work fine for array of three numbers.

- Phanindra May 15, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

try copy pasting in console and this snippet will work for array of three int.

for(var i= 0; i < x.length; i++)
{
a =0;
b=0;
c= 0;
if(i < x.length){
a=i;
}
else{
a=0;
}
if(a < x.length-1){
b=a+1;
}
else{
b=0;
}
if(b < x.length-1){
c=b+1;
}
else{
c=0;
}
if(x[a] == x[b] + x[c]) {
console.log("true" + " " + ":" + x[a] + "=" + x[b] + "+" + x[c]);
}
else{
console.log("false" + " " + ":" + x[a] + "=" + x[b] + "+" + x[c]);
}
}

- Phanindra May 15, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

@Fernando ... Can we do better? O(n^2) is an obvious solution.

- koustav.adorable August 01, 2017 | 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