Amazon OOD:广告定价引擎(基础价、尺寸、定向加价、促销)

OOD:广告定价引擎,支持尺寸的百分比溢价,并讨论计算顺序。

2026-07OAVO Helper · 面经整理

01 · QUESTIONS

面试题目

  • 广告价格由基础价、尺寸修正、定向加价与促销折扣组成。如何为全页广告增加百分比溢价?溢价与折扣的计算顺序如何影响结果?

02 · FOLLOW-UPS

追问

  • 计算顺序对结果的影响

03 · ANSWERS

参考答案

用有序的规则链:基础价之后依次应用尺寸(可含固定加价与百分比溢价)、定向加价、促销。百分比溢价基于当前价格计算,所以它在规则链中的位置会改变最终结果。

解题思路

  • SizeRule 同时支持固定加价与百分比溢价。
  • 全页广告的百分比溢价加在尺寸规则里。
  • 促销放在最后,保证折扣作用于加价之后的价格。

代码

from abc import ABC, abstractmethod
from dataclasses import dataclass, field

BASE = {'banner': 65, 'video': 120, 'sponsored_post': 80}

@dataclass
class Ad:
    ad_type: str
    size: str = 'standard'
    targeting: list = field(default_factory=list)
    advertiser_is_new: bool = False

class Rule(ABC):
    @abstractmethod
    def apply(self, ad, price):
        ...

class SizeRule(Rule):
    def __init__(self, flat=None, percent=None):
        self.flat, self.percent = flat or {}, percent or {}
    def apply(self, ad, price):
        price += self.flat.get(ad.size, 0)
        price += price * self.percent.get(ad.size, 0) // 100
        return price

class TargetingRule(Rule):
    def __init__(self, surcharge):
        self.surcharge = surcharge
    def apply(self, ad, price):
        return price + sum(self.surcharge[t] for t in ad.targeting)

class NewAdvertiserPromo(Rule):
    def __init__(self, discount):
        self.discount = discount
    def apply(self, ad, price):
        return max(0, price - self.discount) if ad.advertiser_is_new else price

class PricingEngine:
    def __init__(self, rules):
        self.rules = rules
    def price(self, ad):
        price = BASE[ad.ad_type]
        for rule in self.rules:
            price = rule.apply(ad, price)
        return price

复杂度

  • 规则数量与定向选项数为线性关系。

追问解答

计算顺序对结果的影响

  • 百分比基于「当前价格」,所以先加固定费用再算百分比,与先算百分比再加固定费用,结果不同。
  • 促销放在所有加价之后,折扣金额更大;放在加价之前则只对基础价打折。
  • 顺序必须与产品约定一致,并用测试固定。

联系 OAVO Helper

想针对这题做模拟练习?

加微信 aasummer 咨询陪跑,我们可以按你的目标岗位安排简历、模拟面试或代投。

加微信 aasummer 咨询陪跑微信:aasummer