CureFit || Interview Experience || Backend Developer
Anonymous User
1857

I was asked this 2 questions. Couldn't solve it in optimal TC/SC. Can anyone please explain?
Also, please suggest from where to learn how to calculate TC? As I was struggling a bit.

  1. Given a matrix of all 0's and an array of indices, find the max number of indices that we can use to mark it as 1 so that we can still reach from 0th row to n-1th row. We can only go through a 0.We can travel in all 4 dirs.
    e.g
0 0 0
0 0 0
0 0 0
0 0 0

arr = [[0,0], [1,1], [2,2]]

we can mark 0,0 as 1, 

1 0 0
0 0 0
0 0 0
0 0 0
We can still reach the last row using 0,1 and 0,2.

we can mark 1,1 as 1.
1 0 0
0 1 0
0 0 0
0 0 0

we can still use 0,2 to reach last row.

mark 2,2 as 1

1 0 0
0 1 0
0 0 1
0 0 0

There's no way to reach the last row.
So ans is 2.

I suggested Brute Force way of marking every int[] from list of indices as 1 and check if we can still reach last row. What would be the TC? I said it was exponential, but he said to reconsider.

  1. Given 2 strings s and p and an index array (again), find the max no of indices you can use to delete a char in s such that p is still a subsequence of s.

e.g

s = aaabb
p = ab
arr = [3,2,0,4]

'.' shows a char is deleted here. 
we can delete s[3] i.e b, so now s = aaa.b, p is still a subsequence of s, ans = 1
we can delete s[2] i.e a, so now s = aa..b, p is still a subsequence of s, ans = 2
we can delete s[0] i.e b, so now s = .a..b, p is still a subsequence of s, ans = 3

we can delete s[4] i.e b, so now s = .a.., p is not a subsequence of s, ans = 3

return 3

Again, gave a Brute Force solution that for each index in the arr we can delete it and check if it's a subsequence.

Please help.

Comments (4)