HackerRank: Staircase

Written by Stephanie Segura | April 30, 2021

Stephanie Segura
2 min readMay 1, 2021

Thank you for visiting this post! Be sure to also take a look at my other work on LinkedIn, Github, and my website.

Picture this scenario for me. You move into this AMAZING house. It’s great, it’s beautiful, it’s…wait is that a staircase?

Oh, no. You’ve never walked on stairs before….why? You never got around to learning it — shhhh just go with it.

Anywho, let’s learn how to conquer stairs via code!

Problem

Please click the link below to view the Problem Statement

Solution

I setup variables and counters for both the spaces and the string element they want us to use. Then I setup a basic for loop: the spaceCount would decrease and the hashTagCount would increase every time it looped through n.

Pretty simple, right? Is there a faster way we can do this though?

SO glad you asked!

My colleague from my study group, Genius Ben we call him, came up with this SUPER amazing solution.

The padStart() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start of the current string.

-MDN Web Docs

Check it out:

Using padStart(), he sets the padding to the amount of n , which is 6, right from the start. He then sets his conditional to loop until “i” is less than or equal to n(6), and every time it repeats, it list’s a ‘#’ in place of the space.

WHAT!? I didn’t even know that padStart() was a thing. That’s the amazing thing about being a developer. You NEVER stop learning!

Conclusion

Thank you so much for stopping by! Starting next Monday, I will begin learning Java. Stay tuned for more Algorithm Content. Java Style!

Please checkout my online portfolio and feel free to connect with me on Linkedin!

--

--