Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

dorinandreidragan
dorinandreidragan

Posted on

     

Solving Problems on HackerRank

Solving Problems on HackerRank

Solving problems on HackerRank is a fantastic way to keep your coding skills sharp. With the right environment setup and a structured approach, you can solve problems efficiently and effectively. In this guide, I’ll walk you through myenvironment setup and demonstratemy approach to solving thestaircase problem using Test-Driven Development (TDD) in C#.

Environment Setup

The following instructions assume you’ll be using C# with Visual Studio Code. Let's get everything set up!

Step 1 - Install .NET SDK

Note: Skip this step if you already have the .NET SDK installed.

First, head over toMicrosoft’s .NET SDK for Visual Studio Code to install the SDK. Then, verify your installation with the following command:

dotnet--info
Enter fullscreen modeExit fullscreen mode

You should see output similar to this:

Note: The output may differ depending on your operating system.

❯ dotnet--info.NET SDK: Version:           8.0.108 Commit:            665a05cea7 Workload version:  8.0.100-manifests.109ff937 ...# content truncated for readability
Enter fullscreen modeExit fullscreen mode

Step 2 - Create a .NET Solution and Test Project

Next, let's create a .NET solution for the problem:

mkdirStaircasecdStaircasedotnet new sln-n Staircase
Enter fullscreen modeExit fullscreen mode

Then, create a test project and add it to the solution:

dotnet new xunit-n Staircase.Testsdotnet sln add Staircase.Tests
Enter fullscreen modeExit fullscreen mode

Finally, build and test the solution to ensure everything is set up correctly:

dotnet builddotnettest
Enter fullscreen modeExit fullscreen mode

If all goes well, your test project should build and run successfully.

Step 3 - Run Tests with Hot Reload

To make your problem-solving process faster, use this command in your solution folder:

dotnet watchtest--project Staircase.Tests
Enter fullscreen modeExit fullscreen mode

This enables hot reloading, so your tests automatically run whenever you make changes, providing instant feedback. 🚀

The Problem-Solving Process

My approach to solving HackerRank problems involves Test-Driven Development (TDD). Here’s a brief overview of TDD and how I apply it.

A Bit of Theory

TDD is guided by three core laws:

  1. Write a failing test before writing any production code.
  2. Write only enough test code to fail, or fail to compile.
  3. Write only enough production code to make the failing test pass.

These laws guide the Red/Green/Refactor cycle:

  1. Create a unit test that fails. 🔴
  2. Write production code to make the test pass. 🟢
  3. Refactor the code to clean up any mess. ✨

For a deeper dive into TDD, I highly recommend readingThe Cycles of TDD by Uncle Bob.

Problem-Solving in Action

Now, let's apply this process to thestaircase problem.

In yourStaircase.Tests project:

  • Create aStaircaseTests.cs file for your tests.
  • Rundotnet watch test --project Staircase.Tests in your terminal.

First Cycle

Let's start with the case when the staircase should beEmptyWhenNoStairs.

🔴 Create a Unit Test That Fails

Write a unit test that fails in theStaircaseTests.cs file:

[Fact]publicvoidEmptyWhenNoStairs(){Assert.True(string.IsNullOrEmpty(Result.staircase(0)));}
Enter fullscreen modeExit fullscreen mode

Your terminal should show output like this:

dotnet watch 🚀 Started  Determining projects to restore...  All projects are up-to-dateforrestore./path/to/staircase/Staircase.Tests/StaircaseTests.cs(10,42): error CS0103: The name'Result' does not existinthe current context[/path/to/staircase/Staircase.Tests/Staircase.Tests.csproj]dotnet watch ❌ Exited with error code 1dotnet watch ⏳ Waitingfora file to change before restarting dotnet...
Enter fullscreen modeExit fullscreen mode

🟢 Write Production Code to Make the Test Pass

Create theResult class and thestaircase method to fix the failing test:

internalclassResult{internalstaticstringstaircase(intn){returnstring.Empty;}}
Enter fullscreen modeExit fullscreen mode

✨ Refactor the Code to Clean Up Any Mess

In this step, there’s no mess to clean up, so let’s move on.

Yay! We’ve solved one case, and our production code works! 🎉

Second Cycle

Now, let's move on to the next case:FirstLevelHasOneStair.

🔴 Create a Unit Test That Fails

[Fact]publicvoidFirstLevelHasOneStair(){Assert.Equal("#",Result.staircase(1));}
Enter fullscreen modeExit fullscreen mode

dotnet watch test should show that this test is failing, which is exactly what we want.

🟢 Write Production Code to Make the Test Pass

Update thestaircase method to make the test pass:

publicstaticstringstaircase(intn){if(n==1)return"#";returnstring.Empty;}
Enter fullscreen modeExit fullscreen mode

Your terminal should now display:

Passed!  - Failed:     0, Passed:     2, Skipped:     0, Total:     2, Duration: 3 ms - Staircase.Tests.dll(net8.0)dotnet watch ⌚ Exiteddotnet watch ⏳ Waitingfora file to change before restarting dotnet...
Enter fullscreen modeExit fullscreen mode

✨ Refactor the Code to Clean Up Any Mess

Again, there’s no need for cleanup at this point, but always make it a habit to check for any mess that needs tidying up.

⚠️ Remember,test code is as important asproduction code. Keep it clean!

Next Cycles

By now, you’ve got the hang of it. Think about the next small step or the next unit test that should fail.

Here are all the steps I followed, each representing a TDD cycle:

[Fact]publicvoidEmptyWhenNoStairs(){Assert.True(string.IsNullOrEmpty(Result.staircase(0)));}[Fact]publicvoidFirstLevelHasOneStair(){Assert.Equal("#",Result.staircase(1));}[Fact]publicvoidHasTheGivenNumberOfStairs(){varstairs=Result.staircase(10).Split('\n');Assert.Equal(10,stairs.Length);}[Fact]publicvoidEachStairHasTheMaximumWidth(){varstairs=Result.staircase(10).Split('\n');foreach(varstairinstairs){Assert.Equal(10,stair.Length);}}[Fact]publicvoidEachStairEndsWithHash(){varstairs=Result.staircase(10).Split('\n');foreach(varstairinstairs){Assert.Equal('#',stair[10-1]);}}[Fact]publicvoidEachStairHasTheCorrespondingNumberOfHashes(){varstairs=Result.staircase(10).Split('\n');for(inti=0;i<10;i++){varexpectedNoOfHashes=i+1;varactualNoOfHases=stairs[i].ToCharArray().Count(c=>c=='#');Assert.Equal(expectedNoOfHashes,actualNoOfHases);}}
Enter fullscreen modeExit fullscreen mode

And here’s my final solution for thestaircase problem:

internalclassResult{internalstaticstringstaircase(intn){varresult=newStringBuilder();for(intcurrentLevel=1;currentLevel<=n;currentLevel++){result.Append(BuildStair(currentLevel,n));}returnresult.ToString();}privatestaticstringBuildStair(intcurrentLevel,intnoOfLevels){varhashes="#".PadLeft(currentLevel,'#');varstair=hashes.PadLeft(noOfLevels,' ');if(currentLevel!=noOfLevels)stair+="\n";returnstair;}}
Enter fullscreen modeExit fullscreen mode

Conclusion

By setting up your environment correctly and following a disciplined approach like TDD, you can tackle HackerRank challenges efficiently. Each small step in TDD, no matter how trivial, builds up to a robust and well-tested solution. The key is to move slowly and methodically—small, deliberate steps will ultimately lead to faster, more reliable problem-solving. Happy coding! 😄

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Passioned about programming, science and music.
  • Location
    Romania
  • Joined

More fromdorinandreidragan

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp