• Study Materials – Company wise
    • Infosys Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Dell Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • TCS Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation – E Books
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements
    • Quant for Placements
  • Register
  • Login
CODE OF GEEKS

We at CODE OF GEEKS, aim at providing best and quality content for our users at no extra cost.

    • Study Materials – Company wise
      • Infosys Archive
      • Accenture Archive
      • AMCAT Archive
      • Capgemini Archive
      • Cisco Archive
      • CoCubes Archive
      • Cognizant(CTS) Archive
      • Dell Archive
      • Deloitte Archive
      • DXC Archive
      • Goldman Sachs Archive
      • Hexaware Technologies Archive
      • LTI Archive
      • MindTree Archive
      • TCS Archive
      • Virtusa Archive
      • Wipro Archive
    • Interview Preparation – E Books
      • C Interview Questions
      • Data Structures Interview Questions
      • DBMS Interview Questions
      • HR Interview Questions
      • Java Interview Questions
      • Operating System Interview Questions
      • Python Interview Questions
      • SQL Query Interview Questions
    • Programming
      • C Programming MCQs
      • C Code Snippets – Output Questions
      • Python Code Snippets – Output Questions
    • Aptitude
      • Verbal Ability for Placements
      • Quant for Placements
    • Register
    • Login
  • [email protected]
  • ..
Login / Register
Apply Now
CODE OF GEEKS
CODE OF GEEKS
  • Study Materials – Company wise
    • Infosys Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Dell Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • TCS Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation – E Books
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements
    • Quant for Placements
  • Register
  • Login
0

Cart

Common Steps

  • March 19, 2020
  • CODE OF GEEKS
  • 3


A child is running up a staircase with n steps and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs.

Input : 4


Output : 7

Input : 3

Output : 4



Solution

def countways(n) : 
	res = [0] * (n + 1) 
	res[0] = 1
	res[1] = 1
	res[2] = 2
	
	for i in range(3, n + 1) : 
		res[i] = res[i - 1] + res[i - 2] + res[i - 3] 
	
	return res[n] 

n = int(input())
print(countways(n)) 


Tags: codevitatcstcs codevita 2019 coding questionstcs codevita 2020tcs codevita coding questionstcs codevita last year coding questions 2019tcs codevita questions 2019tcscodevita
  • Previous How to Prepare for TCS CodeVita 2020 Season 9 Round 1
  • Next Holes and Balls

3 comments on “Common Steps”

  1. Jagarlamudi Mahesh Babu says:
    March 26, 2020 at 10:08 am

    What is the need of res statement
    I need explanation of that program

    Reply
    1. Aman Pandya says:
      April 10, 2020 at 4:28 am

      This was a recursion problem. But recursion took time, so dynamic programming was applied using the res named list. All the results were calculated by the formula and stored for later use. So that again and again already calculated values need not be calculated again

      Reply
  2. prashant says:
    April 12, 2020 at 3:04 pm

    /************************************************************************************************************************
    The question is twisted,but it just wants us to find a fibbonaci series, but instead
    of taking last two digits, we will have to take last three digits.

    proof:
    [LET THE NUMBER OF STAIRS BE NEGATIVE]

    because if the no. of strairs is negative, the kid can’t climb it
    –hence return 0;

    [LET THE NUMBER OF STAIRS BE 0]
    (zero)
    if the number of stairs is 0, the kid cannot climb, but there is at least one floor.
    –hence return 1

    [LET THE NUMBER OF STAIRS BE 1]
    (One)
    if the number of strairs is 1, the kid can only climb it in one way
    i.e just hop on one time
    –hence return 1

    [LET THE NUMBER OF STAIRS BE 2]
    (TWO)
    if the number of stairs is 2, the kid can climb it in [(1,1) steps 0r a single 2 steps]
    i.e in 2 possible ways.
    can you see a pattern here? no? then lets go to the next step.
    –hence return 2

    [LET THE NUMBER OF STAIRS BE 3]
    (THREE)

    possible combination{(1,1,1);(1,1,2);(2,1,1);(3)}, I.e there are 4 combinations.Can you see
    the pattern? Ok, I’ll show it you.

    three= combination in 2(2)+ combinations in 1(1)+ combination in 0(1)=2+1+1=4;
    — hence return 4

    BOOM!!

    // three=combination in two(2)+ combination in one(1)+ combination in zero(1)=4 BIG BOOM!!!!
    // four= combination in three(4)+ combination in two(2)+combination in one(1)=7 SLOW!!!! CLAPS!!
    **************************************************************************************************************************/
    import java.io.*;
    public class Main
    {
    public static void main(String[] args)throws IOException
    {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    int input=Integer.parseInt(br.readLine());
    int a=1; // for 0 steps
    int b=1; // for 1 step
    int c=2; // for 2 steps
    int d=0;
    if(input==1 || input ==2){System.out.print(input);}
    else{
    for(int i=3;i<=input;i++)
    {
    d=c+b+a;
    a=b;
    b=c;
    c=d;

    }

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

CODE OF GEEKS

Subscribe to Newsletter

CODE OF GEEKS

Learn | Code | Achieve

Reach us

[email protected]
We at CODE OF GEEKS, aim at providing quality content to our users at no cost.
CODE OF GEEKS

Important Pages

About us
Advertise
Privacy Policy
Terms and Conditions

Placements – Study Materials

TCS NQT     Wipro     CapGemini
Accenture     MindTree     CTS
DXC     Hexaware Technologies     AMCAT
CoCubes     Goldman Sachs     Dell
Cisco     Deloitte     Virtusa     LTI     Infosys   

Courses

  • Mail Us
  • Login/Register

Recent Posts

TCS Reasoning 2021
  • October 4, 2021
TCS Verbal 2021
  • October 4, 2021

Copyright 2021 CODE OF GEEKS. All Rights Reserved.

  • →