Drill

ProblemsC# › games

Find a card after drawing

easygamesArraysMathC#

A card game draws a number of cards from the top of a deck. Find which card ends up at a given position in the remaining stack.

CardPosition(deck: list<int>, draws: int, position: int) → int

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public int CardPosition(List<int> deck, int draws, int position) {
    
}

Worked examples

CallResult
CardPosition(new List<int> { 10, 20, 30, 40, 50 }, 2, 1)40
CardPosition(new List<int> { 1, 2, 3 }, 0, 0)1
CardPosition(new List<int> { 1, 2, 3 }, 3, 0)-1
CardPosition(new List<int> { 5, 6, 7, 8 }, 3, 0)8

Hint

The answer lives at index draws + position in the original deck, if that index is in bounds.

Reference solution in C#
public int CardPosition(List<int> deck, int draws, int position) {
    int idx = draws + position;
    return idx < deck.Count ? deck[idx] : -1;
}

The same problem in another language

More games problems in C#