# Flipkart – Overall Interview Questions + coding Solutions- codewindow.in ## Filpkart Solution

#### Find the second maximum element in an unsorted array of integers, using single for loop.

``````#include <iostream>
using namespace std;

int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int first = -1, second = -1;
for (int i = 0; i < n; i++) {
if (arr[i] > first) {
second = first;
first = arr[i];
} else if (arr[i] > second && arr[i] != first) {
second = arr[i];
}
}
cout << second << endl;
return 0;
}
/*
OUTPUT
5
7
8
9
3
6
8
*/``````

#### Design a Data Structure SpecialStack that supports all the stack operations like push(), pop(), isEmpty(), isFull() and an additional operation getMin() which should return minimum element from the SpecialStack. All these operations of SpecialStack must be O(1). To implement SpecialStack, you should only use standard Stack data structure and no other data structure like arrays, list, etc.

##### Multi-threading is a process in Java in which two or more threads executes simultaneously for maximum utilization of the CPU.
``````class SpecialStack:
def __init__(self, capacity):
self.stack = []
self.capacity = capacity

def push(self, data):
if len(self.stack) == self.capacity:
return "Stack is full"
minimum = data if len(self.stack) == 0 else min(data, self.stack[-1])
self.stack.append((data, minimum))

def pop(self):
if len(self.stack) == 0:
return "Stack is empty"
return self.stack.pop()

def getMin(self):
if len(self.stack) == 0:
return "Stack is empty"
return self.stack[-1]

def isEmpty(self):
return len(self.stack) == 0

def isFull(self):
return len(self.stack) == self.capacity
``````

#### How do you flat an Array numpy?

##### In NumPy, you can use the `ravel` function to flatten a multi-dimensional array into a one-dimensional array. For example:
``````import numpy as np

# Create a 2-dimensional array
array = np.array([[1, 2, 3], [4, 5, 6]])

# Flatten the array
flattened_array = array.ravel()

print(flattened_array)  # Output: [1 2 3 4 5 6]
``````
##### Alternatively, you can use the `flatten` method, which returns a copy of the flattened array:
``````flattened_array = array.flatten()

print(flattened_array)  # Output: [1 2 3 4 5 6]
``````

#### How do you display a tree spirally?

##### To display a binary tree spirally, you can traverse the tree level by level and reverse the order of nodes at alternate levels. Here's an example implementation in Python:
``````class SpecialStack:
def __init__(self, capacity):
self.stack = []
self.capacity = capacity

def push(self, data):
if len(self.stack) == self.capacity:
return "Stack is full"
minimum = data if len(self.stack) == 0 else min(data, self.stack[-1])
self.stack.append((data, minimum))

def pop(self):
if len(self.stack) == 0:
return "Stack is empty"
return self.stack.pop()

def getMin(self):
if len(self.stack) == 0:
return "Stack is empty"
return self.stack[-1]

def isEmpty(self):
return len(self.stack) == 0

def isFull(self):
return len(self.stack) == self.capacity
``````

#### Encode and Decode a tree.

##### To encode a tree, you can perform a pre-order traversal of the tree and store the values of each node in a string. For example, in the following tree:
``````    1
/ \
2   3
/ \
4   5
``````
##### Here’s an implementation in Python for encoding a tree:
``````class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None

def serialize(root):
if not root:
return "#"
return str(root.val) + " " + serialize(root.left) + " " + serialize(root.right)``````
##### Here’s an implementation in Python for decoding a tree:
``````def deserialize(data):
values = data.split()
index = 0
return helper(values, index)

def helper(values, index):
if values[index] == "#":
return None
node = TreeNode(int(values[index]))
index += 1
node.left = helper(values, index)
index += 1
node.right = helper(values, index)
return node
``````

#### Given an integer n, find the number of structurally unique BSTs possible, of N nodes numbered from 1 to N.

##### Here's the formula:
``````dp[i] = sum(dp[j-1] * dp[i-j] for j in range(1, i+1))
``````
##### Here’s an implementation in Python:
``````def numTrees(n):
dp =  * (n+1)
dp, dp = 1, 1
for i in range(2, n+1):
for j in range(1, i+1):
dp[i] += dp[j-1] * dp[i-j]
return dp[n]
``````
##### The time complexity of this solution is `O(n^2)`. #### Nagarro Solved  #### Automata Fixing  ## We Loveto Support you

Go through our study material. Your Job is awaiting.