Your question is Add Method for Functional Requirement. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
AVIV Group runs real-estate listing portals, and this service tracks a listing's price history. Product wants a way to surface listings whose price dropped meaningfully in the last few days, for a "price drop" feed.
interface PriceEntry {
price: number;
recordedAt: Date;
}
interface Listing {
id: string;
address: string;
priceHistory: PriceEntry[];
active: boolean;
}
class ListingService {
private listings: Listing[] = [];
addListing(listing: Listing): void {
this.listings.push(listing);
}
recordPriceChange(listingId: string, newPrice: number, recordedAt: Date): void {
const listing = this.listings.find(l => l.id === listingId);
if (!listing) return;
listing.priceHistory.push({ price: newPrice, recordedAt });
}
getActiveListings(): Listing[] {
return this.listings.filter(l => l.active);
}
}
Add a method getListingsWithPriceDrop(thresholdPercent, withinDays) that returns active listings whose price dropped by at least thresholdPercent percent within the last withinDays days, comparing the earliest and latest price recorded in that window. Explain your implementation in writing: how you compute the window, how you compare prices, and every edge case you handle, including a listing with only one price point in the window, unsorted priceHistory, an inactive listing, and no drop at all.