A Maniacal Walk- Google Top Interview Questions


Problem Statement :


A person is placed on a list of length length, at index 0, and on each step, they can move right one index or left one index (without going out of bounds), or stay on that index.

Given that the person can take exactly n steps, how many unique walks can the person take and reach back to index 0? Mod the result by 10 ** 9 + 7.

Constraints

length ≤ 1,000

n ≤ 500

Example 1

Input

length = 5

n = 3

Output

4

Explanation

The four actions are:



stay at index 0 3 times in a row.

right, stay, left.

right, left, stay.

stay, right, left.



Solution :



title-img




                        Solution in C++ :

int solve(int length, int n) {
    if (!length) return 0;

    vector<int> dp(length);

    // 1 way to be at the index 0 with 0 steps
    dp[0] = 1;
    const int MOD = 1e9 + 7;

    // calculate answer for each steps
    while (n--) {
        auto dpc = dp;
        for (int i = 0; i < length; ++i) {
            if (i > 0) dpc[i - 1] = (dpc[i - 1] + dp[i]) % MOD;
            if (i < length - 1) dpc[i + 1] = (dpc[i + 1] + dp[i]) % MOD;
        }
        dp = dpc;
    }

    return dp[0] % MOD;
}
                    


                        Solution in Java :

import java.util.*;

class Solution {
    public int solve(int length, int n) {
        if (n == 0)
            return 1;

        int[] dp1 = new int[length];
        int[] dp2 = new int[length];

        final int MOD = (int) 1e9 + 7;

        dp1[0] = 1;

        for (int left = 1; left <= n; left++) {
            for (int i = 0; i < length; i++) {
                dp2[i] = ((dp1[i] + (i - 1 >= 0 ? dp1[i - 1] : 0)) % MOD
                             + (i + 1 < length ? dp1[i + 1] : 0))
                    % MOD;
            }

            for (int i = 0; i < length; i++) {
                dp1[i] = dp2[i];
            }
        }

        return dp1[0];
    }
}
                    


                        Solution in Python : 
                            
class Solution:
    def solve(self, length, n):
        len = min(n + 1, length)
        start = [0] * len
        start[0] = 1
        for i in range(n):
            new = [0] * len
            for j in range(len):
                if j != 0 and j != len - 1:
                    new[j] = start[j - 1] + start[j] + start[j + 1]
                elif j == 0:
                    new[j] = start[j] + start[j + 1]
                else:
                    new[j] = start[j - 1] + start[j]
            start = new
        return start[0] % (10 ** 9 + 7)
                    


View More Similar Problems

Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

View Solution →

Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

View Solution →

Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

View Solution →

Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

View Solution →

Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

View Solution →

Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →