## ASAPInfosystemsPvtLtd Interview Question

Associates**Country:**India

**Interview Type:**Written Test

```
def setNearestMinimum(arr):
op = [99 for x in xrange(len(arr))];
stack = [];
for pos in xrange(len(arr)):
if len(stack) == 0 or pos == 0:
op[pos] = min(op[pos], -1);
stack.append(pos);
else:
while len(stack) > 0 and arr[stack[-1]] > arr[pos]:
stack.pop();
op[pos] = min(op[pos], stack[-1] if len(stack) > 0 else -1);
stack.append(pos);
stack = [];
for pos in xrange(len(arr) - 1, -1, -1):
if len(stack) == 0 or pos == len(arr) - 1:
if op[pos] == -1:
op[pos] = min(op[pos], -1);
stack.append(pos);
else:
while len(stack) > 0 and arr[stack[-1]] > arr[pos]:
stack.pop();
if op[pos] == -1:
op[pos] = stack[-1];
else:
op[pos] = stack[-1] if stack[-1] - pos < pos - op[pos] else op[pos];
stack.append(pos);
for pos in xrange(len(op)):
if op[pos] == -1:
print(-1);
else:
print(arr[op[pos]]);
return;
setNearestMinimum([1, 200, 30, 1000, -6]);
```

Can anyone clarify this problem ? What are we solving?

- Mackoo August 15, 2017