## Amazon Interview Question for SDE-3s

Country: United States
Interview Type: In-Person

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

``````def weave_arrays(array1, array2):
right = deque()
left = deque()
for item in array1:
right.append(item)
for item in array2:
left.append(item)
prefix, results = deque(), []
weave(right, left, prefix, results)
for q in results:
print(q)

def weave(left, right, prefix, results):
if not len(left) or not len(right):
result = deepcopy(prefix)
result.extend(left)
result.extend(right)
results.append(result)
return
weave(left, right, prefix, results)
prefix.pop()
weave(left, right, prefix, results)
prefix.pop()``````

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

The algorithm required here is weaving 2 lists. Consider if you have to weave [1, 2] and [3, 4] together. You can get all the permutations by recursively weaving  and [3, 4] prefixed with one and then recursively weaving [1, 2] and  prefixed with 3

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

val l1 = List('a') //> l1 : List[Char] = List(a)
val l2 = List('b') //> l2 : List[Char] = List(b)
val l3 =List('a', 'b') //> l3 : List[Char] = List(a, b)
val l4 =List('d') //> l4 : List[Char] = List(d)
def f (ls:List[Char], rs:List[Char]):List[List[Char]] = (ls, rs) match {
case (Nil,_) => rs::Nil
case (_,Nil) => ls::Nil
case (x::xs, y::ys) => (f(xs,rs) map (word => x::word)) ++ (f(ls,ys) map (word => y::word))
} //> f: (ls: List[Char], rs: List[Char])List[List[Char]]

f(l1,l2) //> res0: List[List[Char]] = List(List(a, b), List(b, a))
f(l3,l4) //> res1: List[List[Char]] = List(List(a, b, d), List(a, d, b), List(d, a, b))

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

Javascript solution

``````function perm(str1, str2, newArr, m, n, i) {

// Base case: If all characters of str1 and str2 have been
// included in output string, then print the output strin
if (m == 0 && n == 0) {
console.log(newArr)
return;// base condition
}

// If some characters of str1 are left to be included, then
// include the first character from the remaining characters
// and recur for rest
if (m !== 0) {
newArr[i] = str1.charAt(0);
perm(str1.substring(1), str2, newArr, m - 1, n, i + 1);
}

// If some characters of str2 are left to be included, then
// include the first character from the remaining characters
// and recur for rest
if (n !== 0) {
newArr[i] = str2.charAt(0);
perm(str1, str2.substring(1), newArr, m, n - 1, i + 1);
}
}

var str1 = "ab", str2 = "cd", i = 0;
perm(str1, str2, [], str1.length, str2.length, i);``````

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.