# Drop a Ball Down the Stairs - Google Top Interview Questions

### Problem Statement :

```You are given an integer height and a list of integers blacklist.
You are currently standing at height height and you are playing a game to move a small ball down to height 0.

In even rounds (0, 2, 4, 6 etc.) you can move the ball down 1, 2, or 4 stairs down.
In odd rounds, you can move the ball down 1, 3, or 4 stairs.
There are some levels of the stairs where if the ball lands there, it will die as labelled in blacklist.
Return number of ways you can move the ball down to height 0. Mod the result by 10 ** 9 + 7.

Constraints

1 ≤ height ≤ 100,000

0 ≤ b ≤ 100,000 where b is the length of blacklist
Example 1

Input

height = 4

blacklist = [2]

Output

2

Explanation
We can move 4 stairs down on round 0.

We can move 1 stair down on round 0 and then 3 stairs down on round 1.

Example 2

Input

height = 5

blacklist = [0]

Output

0

Explanation

There's no way to reach height 0 since it's on the blacklist.```

### Solution :

```                        ```Solution in C++ :

const int MOD = 1e9 + 7;
const int even[3] = {1, 2, 4};
const int odd[3] = {1, 3, 4};
unordered_set<int> s;
vector<vector<int>> dp;

int f(int h, int round) {
// base case
if (h < 0 or s.count(h)) return 0;

// found a way
if (h == 0) return 1;

// memoization
int& ans = dp[round][h];
if (ans != -1) return ans;
ans = 0;

// even round
if (!round) {
for (auto x : even) {
ans = (ans + f(h - x, round ^ 1)) % MOD;
}
}
// odd round
else {
for (auto x : odd) {
ans = (ans + f(h - x, round ^ 1)) % MOD;
}
}
return ans;
}

int solve(int height, vector<int>& blacklist) {
s.clear();
dp.clear();
dp.resize(2, vector<int>(height + 1, -1));
for (auto x : blacklist) s.insert(x);
return f(height, 0);
}```
```

```                        ```Solution in Java :

import java.util.*;

class Solution {
long MOD = 1000000007;
public int solve(int height, int[] bList) {
DP = new Long[height + 1][2];

Set<Integer> set = new HashSet<>();
for (int num : bList) set.add(num);

return (int) dp(set, height, 0);
}
Long[][] DP;
private long dp(Set<Integer> bList, int curr, int odd) {
if (bList.contains(curr) || curr < 0)
return 0;
if (curr == 0)
return 1;
if (DP[curr][odd] != null)
return DP[curr][odd];
if (odd == 1)
return DP[curr][odd] =
(dp(bList, curr - 1, 0) + dp(bList, curr - 3, 0) + dp(bList, curr - 4, 0))
% MOD;

return DP[curr][odd] =
(dp(bList, curr - 1, 1) + dp(bList, curr - 2, 1) + dp(bList, curr - 4, 1)) % MOD;
}
}```
```

```                        ```Solution in Python :

class Solution:
def solve(self, height, blacklist):
MOD = 10 ** 9 + 7
found = set(blacklist)

if 0 in found:
return 0

@lru_cache(None)
def dp(h, rnd):
if h < 0 or h in found:
return 0

if h == 0:
return 1

ans = 0

if rnd % 2 == 0:
ans += dp(h - 2, rnd ^ 1)
else:
ans += dp(h - 3, rnd ^ 1)

ans += dp(h - 1, rnd ^ 1)
ans += dp(h - 4, rnd ^ 1)

return ans

return dp(height, 0) % MOD```
```

## Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

## Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

## Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

## Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

## Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

## Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from