## JP Morgan Interview Question for Software Engineer / Developers

Country: United States
Interview Type: In-Person

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

``````// ZoomBA
s = "test"
l = s.value
r = [0:#|l|]
permutations = set()
join( @ARGS = list(r) as { l } ) where {
continue ( #|set(\$.o)| != #|l| ) // mismatch and ignore
v = str(\$.o,'') as { l[\$.o] }
continue ( v @ permutations ) // if it already occurred
permutations += v // add them there
}
println( permutations )``````

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

O(n!) time complexity.

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

public class PossibleCombinations {
public static void main(String[] arr) {
combination("", "1234");
}

private static void combination(String prefix, String str) {
int n = str.length();
if (n == 0)
System.out.println(prefix);
else {
for (int i = 0; i < n; i++)
combination(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n));
}
}

}

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

Here is python solution:

``````def next_permutation(data):
size = len(data)
for i in reversed(range(size - 1)):
if data[i] < data[i+1]:
data[i+1:size] = reversed(data[i+1:size])
for j in range(i + 1, size):
if(data[i] < data[j]):
data[i], data[j] = data[j], data[i]
return True
return False

def all_permutation(inp):
ls = list(inp)
while True:
print ''.join(ls)
if not next_permutation(ls):
break
all_permutation('test')``````

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

We can solve the problem in recursive way, but it is dynamic programming so that we won't solve the same problem again and again.
e.g. consider string "abcd". We will make "ab" as prefix and try combination of "cd". Then we will make "ba" as prefix and try combination of "cd". So we are trying combinations of "cd" twice.
So as soon as we complete finding combination of sub-string it should be stored and reused.

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

Recursive solution in Swift with saving the string built so far :

``````var arr = Array("ABC".characters)
var permutations = [String]()

func computePerm(arr: [Character], prefix: String) {
if arr.count == 0 {
permutations.append(prefix)
} else {
var newPrefix = prefix
for i in (0..<arr.count) {
var newArr = arr
newArr.removeAtIndex(i)
computePerm(newArr, prefix: prefix + String(arr[i]))
}
}
}

computePerm(arr, prefix: "")
print(permutations)``````

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

``````public static void permutate(String s) {
if(s == null || s.length() == 0) {
return;
}
permutate(s.toCharArray(), 0);
}
private static void permutate(char[] str, int start) {
if(start >= str.length) {
System.out.println(new String(str));
return;
}
for(int i = start; i < str.length; i++) {
swap(str, i, start);
permutate(str, start+1);
swap(str, i, start);
}
}
private static void swap(char[] input, int i, int j) {
char c = input[i];
input[i] = input[j];
input[j] = c;``````

}

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

//C++ Code
#include<bits/stdc++.h>
using namespace std;

int main()
{
set<string> a;
string s;
cin>>s;
sort(s.begin(), s.end());
a.insert(s); //insert 1st element
while(next_permutation(s.begin(), s.end()))
{
a.insert(s);
}
for(auto x:a)
{
cout<<x<<endl;
}
return 0;
}

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.