πŸ”’ Day 4: HackerRank Practice: Day 4 – Class vs Instance in C#

PriyaPriya
3 min read

πŸ‘‹ Hey folks! I'm currently brushing up on my C# skills through HackerRank’s 30 Days of Code series. Here’s my detailed breakdown and improved solution for Day 4: Class vs Instance.


πŸ“œ Problem Summary

You're asked to create a class Person with:

  1. A constructor that accepts initialAge and validates it.

  2. A method AmIOld() that prints the age category:

    • < 13: "You are young."

    • 13–17: "You are a teenager."

    • >= 18: "You are old."

  3. A method YearPasses() that increases the age by 1.

🟑 Bonus Rule:

If the initial age is negative, print:

Age is not valid, setting age to 0.

After printing the initial age category, simulate the passage of 3 years, then print the updated category again.


πŸ’‘ Approach

  • Constructor: Validate age. If negative, set it to 0 and display a message.

  • AmIOld(): Use if-else to classify age correctly.

  • YearPasses(): Simple method to increment age by 1.

  • Main(): Accepts multiple test cases, simulates aging, and prints results.


πŸ‘¨β€πŸ’» Updated C# Implementation

using System;

class Person

{

private int _age;

public int Age => _age;

// Constructor with age validation

public Person(int initialAge)

{

if (initialAge < 0)

{

Console.WriteLine("Age is not valid, setting age to 0.");

_age = 0;

}

else

{

_age = initialAge;

}

}

// Print age category

public void AmIOld()

{

if (_age < 13)

{

Console.WriteLine("You are young.");

}

else if (_age < 18)

{

Console.WriteLine("You are a teenager.");

}

else

{

Console.WriteLine("You are old.");

}

}

// Increment age by 1

public void YearPasses()

{

_age++;

}

// Entry point: handles multiple test cases

static void Main(string[] args)

{

int testCases = int.Parse(Console.ReadLine());

for (int i = 0; i < testCases; i++)

{

int inputAge = int.Parse(Console.ReadLine());

Person person = new Person(inputAge);

person.AmIOld();

for (int j = 0; j < 3; j++)

{

person.YearPasses();

}

person.AmIOld();

Console.WriteLine(); // Spacing between test cases

}

}

}


πŸ”’ Sample Input

3

-1

10

16

βœ… Sample Output

Age is not valid, setting age to 0.

You are young.

You are young.

You are young.

You are a teenager.

You are a teenager.

You are old.


🧠 Key Takeaways

  • βœ… Constructor validation ensures objects always have a valid state.

  • βœ… Encapsulation using private fields and read-only properties improves class design.

  • βœ… Precise formatting is critical on HackerRank; avoid extra or missing lines.

  • βœ… Edge case testing (e.g., -1, 13, 17, 18) helps catch off-by-one errors.


⚠️ Common Pitfalls

  • ❌ Forgetting to simulate the 3-year time skip.

  • ❌ Printing messages in the wrong order.

  • ❌ Not handling negative input correctly.

  • ❌ Adding extra Console.WriteLine() or missing line breaks.


πŸ“ Final Note

This problem is a great way to reinforce basic class design, conditionals, and method interaction in C#. It also emphasizes clean output formatting β€” a crucial skill in competitive coding platforms.

πŸ’¬ Feel free to share your thoughts, solutions, or questions in the comments below.

πŸ–– Until next time, happy coding!

1
Subscribe to my newsletter

Read articles from Priya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Priya
Priya