Question 51
MediumHow can duplicate permutations be avoided when backtracking over 'AAB'?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
How can duplicate permutations be avoided when backtracking over 'AAB'?
Use a stack
Sort the characters and skip a character equal to the previous one that was not used
Remove duplicates from the input
Run the algorithm twice
Sign up free to see the answer and a step-by-step explanation.
What does tail-call optimisation (in languages that support it) do?
Converts recursion into a hash map
Reuses the current stack frame for a call in tail position, so recursion uses O(1) stack
Makes recursion slower
Removes the base case
Sign up free to see the answer and a step-by-step explanation.
Which is NOT a typical backtracking problem?
Word search in a grid
Generating combinations
N-Queens
Finding the shortest path in a weighted graph with non-negative weights
Sign up free to see the answer and a step-by-step explanation.
Indirect recursion means:
A recursive function with a loop
A function has no parameters
A function calls itself directly
Function A calls B, and B calls A
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def solve(n, r=0, cols=set(), d1=set(), d2=set()):
if r == n:
return 1
c = 0
for col in range(n):
if col in cols or r - col in d1 or r + col in d2:
continue
c += solve(n, r + 1, cols | {col}, d1 | {r - col}, d2 | {r + col})
return c
print(solve(4))
2
0
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def solve(n, r=0, cols=set(), d1=set(), d2=set()):
if r == n:
return 1
c = 0
for col in range(n):
if col in cols or r - col in d1 or r + col in d2:
continue
c += solve(n, r + 1, cols | {col}, d1 | {r - col}, d2 | {r + col})
return c
print(solve(8))
64
92
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def comb(c, t, start=0, cur=[]):
if t == 0:
return [cur]
out = []
for i in range(start, len(c)):
if c[i] <= t:
out += comb(c, t - c[i], i, cur + [c[i]])
return out
print(comb([2, 3, 6, 7], 7))
[[2, 2, 3]]
[[2, 2, 3], [7]]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
calls = 0
def fib(n):
global calls
calls += 1
return n if n < 2 else fib(n - 1) + fib(n - 2)
fib(5)
print(calls)
5
10
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def gen(n, s="", o=0, c=0):
if len(s) == 2 * n:
return [s]
out = []
if o < n:
out += gen(n, s + "(", o + 1, c)
if c < o:
out += gen(n, s + ")", o, c + 1)
return out
print(" ".join(gen(3)))
(()) ()()
((())) (()()) (())() ()(()) ()()()
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of generating all permutations of n distinct elements?
O(n²)
O(n × n!)
O(n!) only, regardless of output
O(2ⁿ)
Sign up free to see the answer and a step-by-step explanation.
1
4
8
40
[[7]]
[[2, 5], [7]]
6
15
((())) (()()) ()(())
((())) ()()()