Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Read no more than you need

    MediumAbout 25 minFull AccessLearn it: Streaming Operators: Where, Select and Take in O(1) Extra Space

    The task

    The fraud screen shows the first few large payments of the day. The payments come from a slow feed: every payment read costs a network call, so the screen must stop reading as soon as it has what it needs. The tests count the payments read.

    Complete FirstLargePayments(IEnumerable<Payment> feed, decimal limit, int count). Payment is a record with an Id and an Amount. Return the first count payments whose Amount is greater than limit, in feed order; if the feed has fewer, return all of them. Read the feed only up to the last payment you return, never further. With count 0, return an empty list without reading anything. With a negative count, throw an ArgumentOutOfRangeException without reading anything. The feed and its payments are never null.

    Signature

    public record Payment(int Id, decimal Amount);
    public static List<Payment> FirstLargePayments(IEnumerable<Payment> feed, decimal limit, int count)

    Examples

    • feed [(1, 50), (2, 500), (3, 20), (4, 700), (5, 900), (6, 800)], limit 100, count 2 → [(2, 500), (4, 700)]. These are the first two payments over 100, and the feed is read only up to payment 4.
    • the same feed, limit 100, count 0 → []. Nothing is asked for, so nothing is read.

    Limits: Feeds of up to 1,000,000 payments.

    The tests check: the payments read in every test, the first large payments, reading stopping at the last match needed, count 0 reading nothing, a negative count refused without reading, fewer matches than asked, a payment exactly at the limit, the feed order kept, and a 1,000,000-payment feed with early matches.

    Hints

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public record Payment(int Id, decimal Amount);
      
      public static class Solution
      {
          // The first `count` payments over the limit, reading the feed only up to the last one returned.
          public static List<Payment> FirstLargePayments(IEnumerable<Payment> feed, decimal limit, int count)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.