## JP Morgan Interview Question for fresherss

Country: United States

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

What is missing are the constraints that array contains only unique numbers in range 0.. n-1. This make the problem very ease. Here is implementation with O(n) time complexity

``````void swap (int i, int j, int[] arr) {
char ch = arr[i];
arr[i] = arr[j];
arr[j] = ch;
}
void swap (int i, int j, char[] arr) {
char ch = arr[i];
arr[i] = arr[j];
arr[j] = ch;
}

void permutate(int[] pos, char[] chars) {
int index = 0;
while (index < chars.length) {
if(index != pos[index]) {
swap(index,pos[index],chars);
swap(index,pos[index],pos);
}
else {
index++;
}
}
}``````

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

The constraints are the things that one should ask the interviewer.

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

``````ArrayList<Integer> arr1 = new ArrayList<Integer>();

ArrayList<String> arr2 = new ArrayList<String>();

Collections.sort(arr1);
Collections.sort(arr2);

System.out.println("ARR1: "+arr1.toString());
System.out.println("ARR2: "+arr2.toString());``````

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

``````public static ArrayList<Integer> sortUpDownArray(ArrayList<Integer> A) {
ArrayList<Queue<Integer>> sortedSubArrays = new ArrayList<Queue<Integer>>();
// True for increasing and false for decreasing
boolean isIncreasing = true;
int start = 0;
for (int i = 1; i <= A.size(); ++i) {
if (i == A.size()
|| // array is ended. Add the last subarray.
A.get(i - 1) < A.get(i) && !isIncreasing
|| A.get(i - 1) >= A.get(i) && isIncreasing) {
if (isIncreasing) {
Queue<Integer> sub = new PriorityQueue<Integer>();
} else {
Queue<Integer> queue = new PriorityQueue<Integer>();
}
start = i;
isIncreasing = (isIncreasing ? false : true);
}
}
return MergeSortedArrays(sortedSubArrays);
}

private static ArrayList<Integer> MergeSortedArrays(
ArrayList<Queue<Integer>> listOfQueues) {
ArrayList<Integer> result = new ArrayList<Integer>();
PriorityQueue<Integer> priQue = new PriorityQueue<Integer>(
listOfQueues.size());
for (Queue<Integer> aQueue : listOfQueues) {
if (aQueue.size() > 0)
}
while (!priQue.isEmpty()) {
Integer poppedItem = priQue.remove();
for (Queue<Integer> queue : listOfQueues) {
if (queue.contains(poppedItem)) {
queue.poll();
if (!queue.isEmpty())
break;
}
}
}
return result;
}``````

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

``````public static ArrayList<Integer> sortUpDownArray(ArrayList<Integer> A) {
ArrayList<Queue<Integer>> sortedSubArrays = new ArrayList<Queue<Integer>>();
// True for increasing and false for decreasing
boolean isIncreasing = true;
int start = 0;
for (int i = 1; i <= A.size(); ++i) {
if (i == A.size()
|| // array is ended. Add the last subarray.
A.get(i - 1) < A.get(i) && !isIncreasing
|| A.get(i - 1) >= A.get(i) && isIncreasing) {
if (isIncreasing) {
Queue<Integer> sub = new PriorityQueue<Integer>();
} else {
Queue<Integer> queue = new PriorityQueue<Integer>();
}
start = i;
isIncreasing = (isIncreasing ? false : true);
}
}
return MergeSortedArrays(sortedSubArrays);
}

private static ArrayList<Integer> MergeSortedArrays(
ArrayList<Queue<Integer>> listOfQueues) {
ArrayList<Integer> result = new ArrayList<Integer>();
PriorityQueue<Integer> priQue = new PriorityQueue<Integer>(
listOfQueues.size());
for (Queue<Integer> aQueue : listOfQueues) {
if (aQueue.size() > 0)
}
while (!priQue.isEmpty()) {
Integer poppedItem = priQue.remove();
for (Queue<Integer> queue : listOfQueues) {
if (queue.contains(poppedItem)) {
queue.poll();
if (!queue.isEmpty())
break;
}
}
}
return result;
}``````

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

we sort second array by comparing ascii values so both implementation will exactly be same except second one needs a cast like (int)B[i].

Question should be to interviewer if second array has all caps and whats expected result if its not.

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.