Country or region and language

    Choose your country or region and your language.

    Suggested for you

      All countries and regions

        Choose your character

        It shows in the top bar and on your profile. Change it any time.

        C# exercises with solutions and edge cases

        Write a solution before reading the approach. Start with the example result, then change one input so a likely shortcut fails.

        Checked 15 November 2026

        A missing reading and a recorded zero

        Write ReadCount for a string-to-integer dictionary. Return the stored number when the key exists, and null when it doesn’t. {"north":0} queried for north returns 0; querying south returns null. Include an empty dictionary.

        The solution uses TryGetValue and returns a nullable integer. Testing value != 0 would discard a valid reading. In the collections lesson, run both cases through the checker.

        A stable report with a tie

        Select active accounts from (Id, Total, Active) rows. Sort by total descending, then ID ascending. For active rows (8,20,true) and (3,20,true), return IDs [3,8]. An inactive row with a larger total must stay out. Empty input returns an empty result.

        Filter, use OrderByDescending, add ThenBy and materialize once. A second OrderBy changes the primary order. Practice this in LINQ ordering.

        A span that preserves the input

        Read the middle two values of [5,8,13,21] and return their sum, 21, without changing the array or copying its elements. Reject inputs shorter than four under an explicit argument contract. A ReadOnlySpan<int> slice gives a view; inspect the original after the call too.

        A view is sufficient because the task only reads. If a later requirement lets callers keep independent data, the ownership contract changes. Practice spans and slicing.

        Cancellation after the first call

        Write an asynchronous loader that passes the caller’s token into a supplied operation. Test a token canceled before entry, then cancel after the operation signals that it started. In both cases, cancellation must reach the caller and any acquired resource must be released.

        Use an explicit start signal in the test rather than guessing a delay. An entry check alone misses the second case. Review CancellationToken.

        Cleanup after a failed report

        Acquire an owned disposable reader and pass it into a report calculation. On success, return the report. If calculation throws, preserve that failure and still dispose the reader once. Verify that a reader supplied by the caller remains usable when the function doesn’t own it.

        A using scope handles the acquired resource’s lifetime. The ownership requirement decides whether the function should dispose it at all. Practice resource disposal and the slow-report case.

        The C# course compiles and checks your answers. The Interview Hub uses different cases; the .NET questions help you explain the reasoning.

        Sources

        Your Work Gym account

        Save your progress and pick up on another device. Your browser's progress comes with you.

        or

        You can keep practicing without an account.

        Help and feedback

        Ask a question, tell us what is not working, or share an idea. We reply by email.

        What is it about?

        We use it only to reply to you.