The hash staircase is HackerRank’s “Staircase” problem: print a right-aligned staircase of # symbols, n steps high, with its base and height both equal to n.
Quick answer: Row i prints n − i spaces followed by i hash symbols. The two counts add up to n, so every row ends in the same column and the steps climb to the right. For n = 6 the first row has five spaces and one hash, and the last row is six hashes.
This program is part of the Symbol Patterns collection.
Table of contents8 sections
Hash Staircase Pattern output
Output (n = 6):
#
##
###
####
#####
######
How the hash staircase pattern works
The staircase is a right-aligned triangle. The trick that trips up many first attempts is that the padding must be printed before the symbols, and that it shrinks while the symbols grow. Printing exactly n characters per row keeps the right edge straight, which is what the problem’s checker looks for.
- Run the outer loop for
ifrom1ton, one row per pass. - Print
n − ispaces. - Print
i#symbols. - Move to the next line.
Key takeaways
- Each row is
n − ispaces plusihashes:ncharacters. - The right edge is straight because every row has the same length.
- In Python,
" " * (n − i) + "#" * ibuilds a row in one line. - It is the hash version of the mirrored right triangle.
Hash Staircase Pattern in C
#include <stdio.h>
int main(void) {
int n = 6;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++)
printf(" ");
for (int j = 1; j <= i; j++)
printf("#");
printf("\n");
}
return 0;
}Hash Staircase Pattern in C++
#include <iostream>
using namespace std;
int main() {
int n = 6;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++)
cout << " ";
for (int j = 1; j <= i; j++)
cout << "#";
cout << endl;
}
return 0;
}Hash Staircase Pattern in Java
public class HashStaircase {
public static void main(String[] args) {
int n = 6;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print("#");
System.out.println();
}
}
}Hash Staircase Pattern in Python
n = 6
for i in range(1, n + 1):
for j in range(1, n - i + 1):
print(" ", end="")
for j in range(1, i + 1):
print("#", end="")
print()Time and space complexity
The program prints one character for each position in the pattern, so for n rows it runs in O(n²) time. It only needs a few loop variables, so the extra memory is O(1).
Frequently asked questions
Why does HackerRank reject my output?
Usually because of trailing spaces or a missing new line. Print exactly n − i spaces and i hashes, then a single new line, with nothing after the last row.
Can I build each row without an inner loop?
Yes. In Python use " " * (n − i) + "#" * i; in Java use " ".repeat(n − i) and "#".repeat(i); in C++ use string(n − i, ' '). The output is the same.
How do I make the staircase go down to the right?
Print i − 1 spaces and n − i + 1 hashes: the steps then start wide on the left and drop towards the right.
How do I let the user choose n?
Read n from the keyboard instead of fixing it in the code: scanf("%d", &n); in C, cin >> n; in C++, new Scanner(System.in).nextInt() in Java and int(input()) in Python.






