• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

July 10, 2014 by Dhaval Dave

Robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it.

We need to Count All paths from top left to bottom right of a mXn matrix,

Method1 Recursive and DP :

We can go backword from (M,N) to (1,1)
As we can take only Up and Left Direction one step , from there we can take either M-1 or N-1

int NumberOfWaysMN(int m, int n)
{
   if (m==1 || n ==1) return 1;
   else {  return NumberOfWaysMN(m-1, n) + NumberOfWaysMN(m, n-1);  }
 
}//Function
As We can see here, there are two recursive functions with m-1, n & m, n-1. Which requires O(N) in worst case space complexity, We can reduce Space complexity and Extra calculations due to recursion  with Dynamic Programming.
Example of Extra Calculations.
for m=5 and n=5, we’ll call

                   Fun(5,5)
                        |
        Fun(4,5)        +            Fun(5,4)
         |                               |
Fun(3,5)+Fun(4,4)                Fun(4,4)+Fun(5,3)

So clearly we are calculating recursive function with 4,4 twice.. which we can reduce if we store results for Fun(4,4)

We need to store result for m and n so we need 2D array to store results.

#include <iostream>
using namespace std;
 
int numberOfPaths(int m, int n)
{
    // Create a 2D table to store results of subproblems
    int pathCount[m][n];
 
    for (int i = 0; i < m; i++)
        pathCount[i][0] = 1;
 
    for (int j = 0; j < n; j++)
        pathCount[0][j] = 1;
 
    for (int i = 1; i < m; i++)
    {
        for (int j = 1; j < n; j++)
             pathCount[i][j] = pathCount[i-1][j] + pathCount[i][j-1];
    }
    return count[m-1][n-1];
}
 
int main()
{
    cout << numberOfPaths(5, 5);
    return 0;
}

Time Complexity O(M*N) and Space Complexity O(M*N)

Variant 2  For N*N :

For an NxN square it takes (2N – 2) steps to move from the upper left corner to bottom right corner.
Of these steps, half must be right and half must be down (N – 1 in each direction).
Knowing this, we can look at it as a string permutation or how many ways can you write “RRRDDD”, etc.
(this would be for N = 4, “RRDD” for N = 3 and so on).
Now we just have to count the number of permutations for this string which in terms of N will be (2N – 2)!/((N – 1)!(N – 1)!).
These numbers match up….
2, 2
3, 6
4, 20
5, 70
6, 252
7, 924

8, 3432

-IF YOU LIKE OUR EFFORT, PLEASE LIKE AND SHARE OUR FB-PAGE AND SITE

Similar Articles

Filed Under: Amazon Interview Question, Data Structure, Interview Questions, Microsoft Interview Questions, problem Tagged With: 2d matrix, Dynamic Programming, Recursion

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Given Set of words or A String find whether chain is possible from these words or not

Given a float number convert it into the string WITHOUT using any inbuilt Function

Count Possible Decodings of a given Digit Sequence

Get K Max and Delete K Max in stream of incoming integers

Reversal of LinkedList

Skiing on Mountains Matrix

Get Minimum element in O(1) from input numbers or Stack

CodeChef Code SGARDEN

Number of Islands BFS/DFS

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

SAP Hiring Off-Campus General Aptitude

Fibonacci Hashing & Fastest Hashtable

Binary Tree Isomorphic to each other

Knight Tour Problem (Graph – Breadth First Search)

Add Sub Multiply very large number stored as string

Stock Buy Sell to Maximize Profit

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

flattens 2 D linked list to a single sorted link list

Trapping Rain Water

There are N nuts and N bolts, u have to find all the pairs of nuts and bolts in minimum no. of iteration

Calculate price of parking from parking start end time prices

Reliance Jio Software Developer Interview Experience

Find loop in Singly linked list

CodeChef’ RRCOPY

SAP Off Campus Hiring_ March 2015 Analytical Aptitude

Binary Tree in Java

Difference between a LinkedList and a Binary Search Tree BST

Leetcode: Merge Intervals

Maximum of all subarrays of size k

write a c program that given a set a of n numbers and another number x determines whether or not there exist two elements in s whose sum is exactly x

Copyright © 2025 · Genesis Framework · WordPress · Log in