Took a hands-on technical online interview, what am I missing?

I am preparing for a job interview in the near future and will conduct a technical audit. I dealt very well with problems other than this ...

The premise of the problem is this: for an array, find the maximum difference for consecutive subsets of an array of size "n". Example

input = [6,8,4,5,3,1,7], n=3
[6,8,4] = the biggest diff = 4 (8-4)
[8,4,5] = the biggest diff = 4 (8-4)
[4,5,3] = 2
[5,3,1] = 4
[3,1,7] = 6
Final return from function:6

The limits of the input data were approximately the following: the length of the array will be less than 100k, n will be less than the length of the array. The function must end within 2 seconds.

I originally wrote this in python, but only got 3/6 of the correct test cases, 3 due to time limitations, so I rewrote hope for better performance in C.

int i,j;
int maxdiff = 0;
int localmax,localmin,localdiff;
for (i=0;i<v_length-d+1;i++){
    localmax = v[i];
    localmin = v[i];
    localdiff = 0;
    for(j=0;j<d;j++){
        if(v[j+i] > localmax){
            localmax = v[j+i];
        }
        if(v[j+i] < localmin){
            localmin = v[j+i];
        }
    }
    localdiff = localmax-localmin;
    if(localdiff > maxdiff){
        maxdiff = localdiff;
    }
}
return maxdiff;

, . 3/6 , 3/6 - .

- ? , ArraySize-n , - , , , , , . ? !

+4
3

O (nlogn), .

+3

, , () , . ( , , , , , .)

max min . O(1), , push_back, pop_front, min max O(1).

, , , ( pop_front pop_back), : , , min max, . , min max, min max, , .

? , , . , . " ", O(1) () () .

: - - , - - . , , . , , . , . , , - , , , .

, O(1), ( ). , O(1) : a pop_front . . (, , O(1).)

, min-max min-max .

. -, , , . ( min-max mins maxes.) , .

, , . , , , , min/max .

, , ++:

#include <algorithm>
#include <vector>

using std::min;
using std::max;

struct minmax { int min; int max; };

int maxrange(const std::vector<int>& v, int n) {
  int sz = v.size();
  n = min(n, sz);
  if (n <= 1) return 0;
  // The stack only needs n - 2 elements. So this could be adjusted.
  minmax* stack = new minmax[n];
  int loback, hiback, lofront, hifront;
  int maxrange = 0;
  for (int s = n - 1, m = 0; s < sz; ++s, --m) {
    if (m == 0) {
      lofront = hifront = v[s];
      loback = hiback = v[s - 1];
      for (int i = 2; i < n; ++i) {
        stack[i - 2] = minmax{loback, hiback};
        loback = min(loback, v[s - i]);
        hiback = max(hiback, v[s - i]);
      }
      m = n - 1;
    } else {
      lofront = min(lofront, v[s]);
      hifront = max(hifront, v[s]);
      loback = stack[m-1].min;
      hiback = stack[m-1].max;
    }
    maxrange = max(maxrange, max(hifront, hiback) - min(lofront, loback));
  }
  delete[] stack;
  return maxrange;
}
+3

Input:

int inp[]={...},n=...;
  • int index

    const int N=sizeof(inp)/sizeof(inp[0]);
    int ix[N];
    for (int i=0;i<N;i++) ix[i]=i;
    
  • inp[N] ix[N] ( inp[N] )

    • i={0,1,2,...,N-2} (inp[ix[i]]<=inp[ix[i+1]])==true
    • O(N*log(N))
  • 1<n<=N int i0=0,1,2,...N-n :

    int i1=i0+n-1; // valid indexes for subset are i0<=i<=i1;
    int min=0,max=0;
    for (i=  0;i< N;i++) if ((ix[i]>=i0)&&(ix[i]<=i1)) { min=inp[ix[i]]; break; }
    for (i=N-1;i>=0;i--) if ((ix[i]>=i0)&&(ix[i]<=i1)) { max=inp[ix[i]]; break; }
    int diff=max-min;
    
  • 3

    • max diff...

[]

  • it's faster for a larger one n, but slower for a smaller onen
  • so it’s best to use your approach, and this is in accordance with n size treshold.
0
source

All Articles