🎓 HackerRank Practice: Student Marks Manager using C# Lists

Working with Lists is one of the most common exercises in coding challenges. Lists in C# are dynamic collections that allow adding, removing, and inserting elements easily. Let’s solve a simple HackerRank-style problem step by step.


📌 Problem Statement: Student Marks Manager

You are given student marks. Perform operations using a List<int>.

Input
5
10 20 30 40 50
3
Add 60
Remove 20
Insert 25 2

Output
10 25 30 40 50 60

Explanation

  • Start: [10, 20, 30, 40, 50]
  • Add 60 → [10, 20, 30, 40, 50, 60]
  • Remove 20 → [10, 30, 40, 50, 60]
  • Insert 25 at index 2 → [10, 25, 30, 40, 50, 60]

✅ This tests:

  • Index-based access
  • Add / Remove operations
  • Insert at a given index

🛠️ Solution in C#

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        // Input: number of students
        int n = int.Parse(Console.ReadLine());

        // Input: student marks
        string[] input = Console.ReadLine().Split(' ');
        List<int> marks = new List<int>();

        for (int i = 0; i < n; i++)
            marks.Add(int.Parse(input[i]));

        // Input: number of operations
        int noOfOperations = int.Parse(Console.ReadLine());

        // Perform operations
        for (int i = 0; i < noOfOperations; i++)
        {
            string[] command = Console.ReadLine().Split();
            string operation = command[0];
            int value = int.Parse(command[1]);

            if (operation == "Add")
            {
                marks.Add(value);
            }
            else if (operation == "Remove")
            {
                marks.Remove(value);
            }
            else if (operation == "Insert")
            {
                int index = int.Parse(command[2]);
                marks.Insert(index, value);
            }
        }

        // Output final list
        Console.WriteLine(string.Join(' ', marks));
    }
}

🔎 Walkthrough

  1. Reading Input
    • First line → number of students n.
    • Second line → list of marks.
    • Third line → number of operations.
    • Following lines → commands (Add, Remove, Insert).
  2. Using List<int>
    • Add(value) → appends an item.
    • Remove(value) → removes the first occurrence of an item.
    • Insert(index, value) → inserts at a specific position.
  3. Output
    • After performing all operations, we print the final list using string.Join(' ', marks).

🚀 Key Learnings

  • List<T> is flexible compared to arrays.
  • You can directly use Add, Remove, and Insert methods.
  • Indexing in C# Lists starts from 0.

This type of problem is great for practicing basic data structures and input/output handling in coding contests.

Comments

Popular posts from this blog

Factory Method Design Pattern in .NET — Real-Time Finance Example

Logging in .NET Core: Built-in Logging vs Serilog with Full Implementation Guide

Implementing Single Sign-On (SSO) in .NET Core and Angular