Nutanix Interview Question for SDE-2s


Country: United States




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

Just use a stack.

function rangeAsMax(nS : number[]) {
   let stack = new Stack<number>();
   let result = new Array<[number,number]>(nS.length);
   for (let i = 0; i < nS.length; i += 1) {
      while (!stack.isEmpty() && nS[stack.top() <= nS[i]) {
         let j = stack.pop();
         result[j][1] = i - 1;
      }
      if (!stack.isEmpty()) {
         assert(nS[stack.top()] > nS[i]);
         result[i] = [stack.top() + 1, undefined];
      } else result[i] = [0, undefined];
      stack.push(i)
   }
   while (!stack.isEmpty()) {
      let j = stack.pop();
      result[j][1] = stack.length - 1;
   }
   return result;
}

- xiongmao December 12, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

There are some typos in the code. The last part where the stack is drawn down should be

while (!stack.isEmpty()) {
      let j = stack.pop();
      result[j][1] = nS.length - 1;
   }

- xiongmao December 12, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi, What language is this? thnks

- trx32 December 13, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

javacript

- sid December 21, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

javascript

- sid December 21, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This doesn't give correct result. In above example, while processing 3, top of stack in 5.. not 4. So this doesn't work.

- Coder December 26, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here's what I'd do
for position
1 loop min from pos to 0(array index -1) and stop when array[min] becomes greater than array[pos](our given num) this will be the index at which our condition breaks so min+1 is in range where array[pos] is the largest num in range
2 similarly loop max from pos to array length, so when our condition breaks max-1 will contain the pos of the last number thats smaller than our given num
3 convert into our range that will be in [1 to array size] from machine index which will be in [0 to array size -1] by adding 1 to min+1 and max-1... which is min+2 and max
heres how the program goes

static int[] arr = {1, 5, 4, 3, 6};

public static void main(String[] args) {
    for (int i = 0; i < arr.length; i++) {
        printREIsMax(i);
    }
}

private static void printREIsMax(int pos) {
    int min = pos, max = pos;
    while (min >= 0 && arr[min] <= arr[pos]) min--;
    while (max < arr.length && arr[max] <= arr[pos]) max++;
    System.out.println(arr[pos] + "[" + (min + 2) + "," + (max) + "]");
}

- PeyarTheriyaa December 12, 2018 | 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