## Nutanix Interview Question for SDE-2s

• 1
of 1 vote

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] = 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] = stack.length - 1;
}
return result;
}``````

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

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] = nS.length - 1;
}``````

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

Hi, What language is this? thnks

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

javacript

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

javascript

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

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

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) + "]");
}``````

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.

### 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.