wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

sample

Total questions: 1

Worksheet time: 30secs

Name
Class
Date
1.

1.    Your team is building a live bidding system for an online marketplace. Bids arrive from many users globally (threads), and the system must maintain the current highest bid for each auction item — while also logging each bid for auditing. The requirement: even under heavy concurrency, the highest bid must never be lost or overwritten incorrectly.

Pseudo-code:

class AuctionItem {

    private volatile double highestBid = 0.0;

    private List<Bid> auditLog = new ArrayList<>();

 

    void submitBid(double amount, String bidderId) {

        if (amount > highestBid) {

            highestBid = amount;

            logBid(amount, bidderId);

        }

    }

    void logBid(double amount, String bidderId) {

        auditLog.add(new Bid(amount, bidderId));

    }

    double getHighestBid() { return highestBid; }

}

for each incomingBid in bidStream:

    start new Thread(() -> auctionItem.submitBid(incomingBid.amount, incomingBid.bidderId));

After running under heavy load, you find that occasionally:

·         A bid that was lower than a previous highest bid becomes the highestBid.

·         AuditLog entries are correct (every submitted bid is logged).

·         Threads appear to finish without crash or exception.

Question:
Identify the root cause of the anomalous behaviour, and propose a corrective design change.

a)

1

b)

2

c)

3

d)

4