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

Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →

Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →

Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →

Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →

Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →