Problemlocal marketplace query index

Session: Sign in to solve

Solution.txt

Local Marketplace Query Index

Problem

Design LocalMarketplaceQueryIndex to maintain current local seller offers and corrected completed orders. It uses a supplied DistancePolicy to return independent cheapest and nearest item results and reports completed-order statistics for inclusive time windows.

Requirements

  • Seller location. registerSeller creates a seller or replaces an existing seller's current location. Existing offers and completed orders remain attached to that seller.
  • Current offer. publishOffer creates or replaces the one current offer for its (sellerId, itemId) pair.
  • Unknown seller. publishOffer and recordCompletedOrder raise InvalidArgument for an unknown seller and change no seller, offer, or order state.
  • Missing item. queryItem returns no result when the item has no current offer.
  • Cheapest offer. The cheapest view contains the unique lowest-price current offer for the item.
  • Nearest eligibility. The nearest view considers only sellers with a current offer for the requested item.
  • Nearest distance. The nearest view contains the eligible offer with minimum distance according to the supplied policy.
  • Order correction. recordCompletedOrder creates an order or atomically replaces the seller, completion timestamp, and value of an existing orderId. Only the current record contributes to summaries.
  • Inclusive window. A current order contributes if and only if startInclusive <= completedAt <= endInclusive.
  • Order count. count equals the number of current orders in the window.
  • Total value. totalValueCents equals the exact integer sum of valueCents for current orders in the window.
  • Nonempty average. For a nonempty window, averageValueCents equals totalValueCents / count as a real number.
  • Empty average. For an empty window, averageValueCents equals 0.0.

API

Each testcase supplies this exact constructor transport:

{"distancePolicy":{"kind":"weighted-manhattan","xWeight":1,"yWeight":1}}

The driver constructs DistancePolicy so distance(fromX, fromY, toX, toY) equals xWeight * abs(fromX - toX) + yWeight * abs(fromY - toY).

OfferView contains sellerId, itemId, priceCents, sellerX, sellerY, and distance. ItemQueryResult contains cheapest and nearest offer views. OrderSummary contains count, totalValueCents, and averageValueCents.

SignatureReturnsBehavior
LocalMarketplaceQueryIndex(distancePolicy: DistancePolicy)Not applicableCreates an empty index and retains the non-null policy.
registerSeller(sellerId: string, x: integer, y: integer)voidCreates or relocates the seller.
publishOffer(sellerId: string, itemId: string, priceCents: integer)voidCreates or replaces the seller-item offer; an unknown seller raises InvalidArgument without mutation.
queryItem(itemId: string, userX: integer, userY: integer)ItemQueryResult?Returns independent cheapest and nearest current views, or no result. Each view contains distance computed for this call.
recordCompletedOrder(orderId: string, sellerId: string, completedAt: integer, valueCents: integer)voidCreates or atomically corrects an order; an unknown seller raises InvalidArgument without mutation.
summarizeCompletedOrders(startInclusive: integer, endInclusive: integer)OrderSummaryReturns statistics for current orders in the inclusive window.

Examples

Using weighted Manhattan distance with both weights equal to 1:

StepOperationResult
1registerSeller("A", 0, 0)Seller A is registered.
2registerSeller("B", 2, 1)Seller B is registered.
3publishOffer("A", "book", 400)A's current offer is 400 cents.
4publishOffer("B", "book", 500)B's current offer is 500 cents.
5queryItem("book", 3, 1)Cheapest is A; nearest is B at distance 1.
6registerSeller("A", 3, 1)A relocates without losing its offer.
7queryItem("book", 3, 1)Both views select A; its distance is 0.

After recording o1 at time 10 for 1000 cents and o2 at time 20 for 500 cents, summarizeCompletedOrders(10, 20) returns {count: 2, totalValueCents: 1500, averageValueCents: 750.0}. Correcting o2 to time 30 and 700 cents changes summarizeCompletedOrders(10, 20) to {count: 1, totalValueCents: 1000, averageValueCents: 1000.0}.

Constraints

  • Identifier lengths are from 1 through 64 and contain printable non-whitespace ASCII characters.
  • Coordinates are from -1,000,000 through 1,000,000.
  • 1 <= priceCents <= 1,000,000,000 and 0 <= valueCents <= 1,000,000,000.
  • Timestamps and window endpoints are from 0 through 1,000,000,000,000, with startInclusive <= endInclusive.
  • A distance-policy object contains exactly kind, xWeight, and yWeight; kind is "weighted-manhattan" and each weight is from 1 through 1,000,000.
  • Every valid queryItem call has a unique lowest price and a unique lowest policy distance among current offers for its item. Inputs with a tie are outside the exercise.
  • At most 10,000 sellers, 100,000 current offers, 100,000 current orders, and 200,000 public method calls exist.
  • Money, timestamp, and distance values use signed 64-bit integers. Exact totals remain below 9,000,000,000,000,000.

Notes

Calls are sequential. Each testcase supplies one non-null distance policy, and different testcases may use different policies. InvalidArgument maps to ValueError in Python, IllegalArgumentException in Java, and std::invalid_argument in C++.

PRIVATE WORKSPACE

Checking your session…

The statement is public. The editor, editorial, submissions, and saved work are private.