Saturday 7 July 2018

Amazons coding interview question on possible ways of reaching to top of a stair case with N steps with N step overs

Actually I came across a video in youtube giving an overview on this coding interview question by amazon. I have gone through the question and started coding in c#. Here I solved the problem in my own way to test my skills in programming.

The question is about to find possible number of ways from starting point, all the way to the end with a set of possible step overs. To get a quick understanding I provided an image.

The code I have pasted below is basically a console app created in visual studio. You can create a new console app in your local and paste this code there and run it. If you can refactor the code just refactor it and paste it in the comment below.


To know the how the code works check this link: StairNWays(.net fiddle)


using System;
using System.Collections.Generic;
using System.Linq;

namespace stairNways
{
    class Program
    {
        static void Main(string[] args)
        {
            int noOfPossibilities;
            int noOfSteps;
            List<int> combinationsList = new List<int>();
            List<int> noOfWaysList = new List<int>();
            bool reached = false;
            Console.Write("Enter the number of steps: ");
            noOfSteps = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter the number of possible step overs that you want to make: ");
            noOfPossibilities = Convert.ToInt32(Console.ReadLine());
            if (noOfPossibilities > 0)
            {
                Console.WriteLine("Enter the step over values: ");
                for (int i = 0; i < noOfPossibilities; i++)
                {
                    Console.Write("> ");
                    combinationsList.Add(Convert.ToInt32(Console.ReadLine()));
                }
                Console.WriteLine("You can reach your destination in following possible ways");
                do
                {
                    noOfWaysList = GetArray(noOfWaysList, combinationsList, noOfSteps);
                    reached = checkReached(noOfWaysList, noOfSteps);
                } while (!reached);
                Console.WriteLine(">>>>>>>>>>>>>>>>{0}<<<<<<<<<<<<<<<", noOfWaysList.Count());
            }
            else
            {
                Console.WriteLine("you entered an invalid nuber");
            }
            Console.ReadKey();
        }

        private static bool checkReached(List<int> noOfWaysList, int noOfSteps)
        {
            bool reached = true;
            foreach (var nwl in noOfWaysList)
            {
                if (nwl != noOfSteps)
                    reached = false;
            }
            return reached;
        }

        private static List<int> GetArray(List<int> possibleWaysList, List<int> stepOvers, int noOfSteps)
        {
            if (possibleWaysList == null || possibleWaysList.Count() == 0)
            {
                foreach (var stepOver in stepOvers)
                {
                    possibleWaysList.Add(stepOver);
                }
            }
            else
            {
                List<int> dummyList = new List<int>();
                foreach (var pwl in possibleWaysList)
                {
                    if (pwl == noOfSteps)
                    {
                        dummyList.Add(pwl);
                        continue;
                    }

                    foreach (var so in stepOvers)
                    {
                        if ((pwl + so) <= noOfSteps)
                        {
                            dummyList.Add(pwl + so);
                        }
                    }
                }
                return dummyList;
            }
            return possibleWaysList;
        }
    }
}




No comments :

Post a Comment