Fork me on GitHub

Project Notes

#477 climbStairs

Using pascal to climb stairs; cassidoo’s interview question of the week (2026-08-31).

Notes

The interview question of the week (2026-08-31):

Given an integer n representing the number of steps in a staircase, return the number of distinct ways you can reach the top if you can climb either 1 or 2 steps at a time.

Example:

climbStairs(2)
> 2

climbStairs(4)
> 5

climbStairs(10)
> 89

Thinking about the Problem

This smells very much like a simple problem in permutations and combinations.

To reach step n, the previous move must have been either:

  • a 1-step move from n-1, or
  • a 2-step move from n-2.

Therefore f(n)=f(n-1)+f(n-2) with f(0)=1, f(1)=1. So:

n ways
0 1
1 1
2 2
3 3
4 5
5 8
6 13
7 21

And that is… the Fibonacci sequence!

A First Go

Using Pascal this time with a simple Fibonacci algorithm to iteratively sum the preceding two elements:

function ClimbStairs(n: Integer): Int64;
var
  a, b, next: Int64;
  i: Integer;
begin
  a := 1;  // f(0)
  b := 1;  // f(1)

  for i := 1 to n do
  begin
    next := a + b;
    a := b;
    b := next;
  end;

  ClimbStairs := a;
end;

Compile with fpc, the Free Pascal compiler:

$ fpc challenge.pp
Free Pascal Compiler version 3.2.2 [2025/09/11] for aarch64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Darwin for AArch64
Compiling challenge.pp
Assembling challenge
Linking challenge
-macosx_version_min has been renamed to -macos_version_min
ld: warning: -multiply_defined is obsolete
37 lines compiled, 0.6 sec

Let’s try…

$ ./challenge
Usage: challenge <n>
$ ./challenge 2
2
$ ./challenge 4
5
$ ./challenge 10
89

Looking good!

Final Code

challenge.pp:

program challenge;

uses
  SysUtils;

function ClimbStairs(n: Integer): Int64;
var
  a, b, next: Int64;
  i: Integer;
begin
  a := 1;  // f(0)
  b := 1;  // f(1)

  for i := 1 to n do
  begin
    next := a + b;
    a := b;
    b := next;
  end;

  ClimbStairs := a;
end;

var
  n: Integer;

begin
  if ParamCount < 1 then
  begin
    WriteLn('Usage: challenge <n>');
    Halt(1);
  end;

  n := StrToInt(ParamStr(1));

  WriteLn(ClimbStairs(n));
end.

Credits and References

About LCK#477
Pascalcassidoo

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

Project Source on GitHub Return to the LittleCodingKata Catalog
About LittleCodingKata

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.

Follow the Blog follow projects and notes as they are published in your favourite feed reader