Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Ways to climb with given step sizes

    EasyAbout 15 minFull AccessLearn it: Climbing Stairs: the 'hello world' of DP

    The task

    A fitness app shows how many different ways there are to climb a staircase of n steps when every stride covers one of the allowed numbers of steps.

    Complete WaysWithSteps(int n, int[] steps). Return how many ordered sequences of strides add up to exactly n, each stride taken from steps. The sizes in steps are positive and distinct. Order matters: 1 then 2 and 2 then 1 are different climbs. For n = 0 the empty climb counts once, even when steps is empty. Throw ArgumentOutOfRangeException for a negative n. The answer fits in a long.

    Signature

    public static long WaysWithSteps(int n, int[] steps)

    Examples

    • 4, [1, 2] → 5. 1+1+1+1, 1+1+2, 1+2+1, 2+1+1 and 2+2.
    • 0, [1, 2] → 1. Doing nothing is the one way to climb no steps.

    Limits: n up to 10,000; up to 20 step sizes.

    The tests check: n = 0 with and without step sizes, no step sizes, a size larger than n, sizes that cannot make n, counts larger than int, a negative n, and n = 50 with sizes 1 and 2 in time.

    Hints

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public static class Solution
      {
          // How many ordered sequences of strides from steps add up to exactly n.
          public static long WaysWithSteps(int n, int[] steps)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.