Pergunta de entrevista da empresa Bloomberg

Given an array like [-1, 2, 3, 5, 4, 6] and it should print the 3 that is order of the array is equal to array element

Respostas da entrevista

Sigiloso

12 de ago. de 2016

int a[] = { -1, 2, 3, 5, 4, 6}; for(int i=0; i < a.length; i++) { if(a[i]==i+1) System.out.println(a[i]); } then interviewer asked to improve the performance for large arrays. So how to improve array iteration in above case in java.?

4

Sigiloso

19 de jul. de 2017

#include using namespace std; int main() { int a[] = {-1,1,3,5,4,6}; int size = sizeof(a)/sizeof(int); for(int i = 0; i < size; i++) { if(i+1 == a[i]) cout << a[i] << " "; } return 0; }

1

Sigiloso

23 de ago. de 2016

Was the array ordered?

Sigiloso

24 de ago. de 2016

Yes, that is array order.

Sigiloso

24 de ago. de 2016

ordered meaning sorted? the example you have here is not a sorted array

Sigiloso

13 de set. de 2016

If it was sorted, all you needed to do was a binary search

2

Sigiloso

6 de jan. de 2017

#python ar1 = [-1, 2, 3, 5, 4, 6] ar2 = range(len(ar1)) cond_ar = map(lambda x,y:(x,x==y),ar1,ar2) # map element to equality check for t in filter(lambda x: x[1],cond_ar): print t[0]