[Python] My solution for IPO (Initial Public Offering) auction interview question

Discussão em 'Python' iniciado por Stack, Maio 6, 2025.

  1. Stack

    Stack Membro Participativo

    [​IMG]

    *TLDR:

    Input:

    bids = list of lists of ints representing [userid, # shares, $bid, timestamp]

    totalShares = total # of shares to be distributed.

    TODO: distribute shares amongst bidders and return userids of bidders that got 0 shares. Share distribution logic: 1) Bidder with highest offer gets all the shares they bid for, and then 2) if there are ties in $ bid price, assign shares to earlier bidder

    For the above question, I feel like the solution I came up with is relatively simple but it seems to pass all edge cases I can think of. The only questionable situation I can think of is like

    • Bid price and times are the same and there aren't enough shares for all bidders ie: bids = [[0,2,10,0], [1,2,10,0]] and totalShares = 2. I'm unclear if like 1 share should be given to each or userid 0 just gets both.

    Does anyone see if my solution could be optimized in anyway?

    def getUnallocatesUsers(bids, totalShares):
    s = 0
    for b in bids:
    s += b[1]
    if totalShares >= s: return [] # no losers because enough shares to go around

    bids.sort(key = lambda x: (-x[2],x[3])) # sort by highest bid, then timestamp for ties
    losers = []
    for b in bids:
    if totalShares <= 0: losers.append(b[0])
    else:
    totalShares -= b[1]
    return losers

    Continue reading...

Compartilhe esta Página