## Interview Question

Students**Country:**India

```
def allPermutations(level: Int, prefix: Int, remaining: Array[Int]): List[Int] = {
if (remaining.isEmpty) return List[Int]()
println("level = " + level + " prefix = " + prefix + " remaining = " + remaining.mkString(","))
var subsets = List[Int]()
for (i <- 0 until remaining.length) {
val current = remaining(i)
val subset = prefix * math.pow(10, level).toInt + current
println("current = " + current + " subset = " + subset)
subsets = subsets.:+(subset)
val split = remaining.slice(0, i) ++ remaining.slice(i+1, remaining.length)
val childRemaining = split
val childLevel = 1 // level // + 1
subsets = subsets ++ allPermutations(childLevel, prefix = subset, childRemaining)
}
return subsets
}
val output = allPermutations(0, prefix = 0, remaining = Array(4, 5, 6, 7))
```

```
def allPermutations(level: Int, prefix: Int, remaining: Array[Int]): List[Int] = {
if (remaining.isEmpty) return List[Int]()
println("level = " + level + " prefix = " + prefix + " remaining = " + remaining.mkString(","))
var subsets = List[Int]()
for (i <- 0 until remaining.length) {
val current = remaining(i)
val subset = prefix * math.pow(10, level).toInt + current
println("current = " + current + " subset = " + subset)
subsets = subsets.:+(subset)
val split = remaining.slice(0, i) ++ remaining.slice(i+1, remaining.length)
val childRemaining = split
val childLevel = 1 // level // + 1
subsets = subsets ++ allPermutations(childLevel, prefix = subset, childRemaining)
}
return subsets
}
val output = allPermutations(0, prefix = 0, remaining = Array(4, 5, 6, 7))
```

```
def allPermutations(level: Int, prefix: Int, remaining: Array[Int]): List[Int] = {
if (remaining.isEmpty) return List[Int]()
println("level = " + level + " prefix = " + prefix + " remaining = " + remaining.mkString(","))
var subsets = List[Int]()
for (i <- 0 until remaining.length) {
val current = remaining(i)
val subset = prefix * math.pow(10, level).toInt + current
println("current = " + current + " subset = " + subset)
subsets = subsets.:+(subset)
val split = remaining.slice(0, i) ++ remaining.slice(i+1, remaining.length)
val childRemaining = split
val childLevel = 1 // level // + 1
subsets = subsets ++ allPermutations(childLevel, prefix = subset, childRemaining)
}
return subsets
}
val output = allPermutations(0, prefix = 0, remaining = Array(4, 5, 6, 7))
```

- nil July 26, 2019