Pergunta de entrevista da empresa Meta

Except experience related questions I was asked coding question and I was supposed to write the solution to the problem in any programming language in a simple web-based code editor. The question was: given and array of positive integers and another integer find whether there is a consecutive range in the array, so that the sum of integers in the range is equal to the given number. They put an emphasis on delivering working, efficient solution. In my opinion this kind of interview requires a thorough preparation.

Respostas da entrevista

Sigiloso

3 de nov. de 2015

Two answers, 2nd is O(n) {code} boolean findSum (int[] a, int index, int n) { if (n == 0) return true; if (index > a.size) return false; for (int i = index; i n) sum -= a[sp++]; } return false; } {code}

1

Sigiloso

3 de nov. de 2015

Corrected a bug in solutions 2 ab

Sigiloso

3 de nov. de 2015

Corrected a bug in solution 2 above: boolean findSum (int[] a, int n) { int sum = 0, sp = 0; for (int i = 0; i n) { sum -= a[sp]; if (sp < i) { sp++; i--; // this is because i increments for each loop continue; } } sum += a[i]; if (sum == n) return true; } return false; }

Sigiloso

1 de mai. de 2017

public static void findSum(List A, int k){ int runningSum = 0, fast = 0, slow = 0; while(fast k && slow <= fast) { runningSum -= A.get(slow++); } if(runningSum == k){ System.out.println(slow + " : " + (fast-1)); break; } } }

Sigiloso

21 de out. de 2015

Efficient solution you say? Its easy to do it in O(N^2) but I think you mean the O(N*logN) solution. It took me about 5-10 minutes to come up with m, but I am not sure I would be able to write the full source code for this solution in a short period of time. If you did it - then thumbs up man!