> For the complete documentation index, see [llms.txt](https://junnie.gitbook.io/wayfair/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://junnie.gitbook.io/wayfair/vo/coding/coupon.md).

# Coupon

Coding: 给两个array. 每个array 的每个元素是一个map。

一个array 里放的是coupons,&#x20;

另一个放category 和parent category。如下：&#x20;

coupons: \[&#x20;

{"Coupon Name" : "50% off"， “Category Name”: "Bedding"},&#x20;

{"Coupon Name" : "BOGO"， “Category Name”: "Kitchen"}&#x20;

]&#x20;

category: \[&#x20;

{"Category Name" : "Comforter"， “Parent category Name”: "Bedding"},&#x20;

{"Category Name" : "Kitchen"， Parent category Name”: null},&#x20;

{"Category Name" : "Patio"， “Parent category Name”: "Garden"}&#x20;

]&#x20;

given a category name, return its coupon if there is one for the category. Otherwise, check its parent category to see if there's a coupon for it. If so, return the coupon name. Repeat this process until there is no parent category, then return null.

follow up是： 1. 如何更高效低把字符串的coupon，category信息转换成对象？ 这里楼主只想当用JSON字符串映射到entity的做法。写到一半面试官说时间不多了，直接跳到第二个follow-up。2. coupon加多一个日期，要求返回最近的一个coupon。

```
import java.util.*;
import java.text.*;

public class CouponRetrieve {
    public static void main(String[] args)
    {
        Coupon[] coupons =  { new Coupon("50% off","Bedding"), 
                              new Coupon("BOGO","Kitchen")};
        Category[] categories =  { new Category("Comforter", "Bedding"), 
                                   new Category("Kitchen", null), 
                                   new Category("Patio", "Garden") };

        CouponWithDate[] couponsWithDate = {new CouponWithDate("50% off","Bedding", "2022-03-04"), 
                                             new CouponWithDate("BOGO","Kitchen", "2022-05-06"),
                                             new CouponWithDate("60% off","Bedding", "2022-03-09")};
                                   
        String coupon1 = findCoupon("Comforter", coupons, categories);
        System.out.println(coupon1);
        String coupon2 = findCoupon("Patio", coupons, categories);
        System.out.println(coupon2);

        String coupon3 = findLatestCoupon("Comforter", couponsWithDate, categories);
        System.out.println(coupon3);
    }
    //given a category name, return its coupon if there is one for the category. 
    //Otherwise, check its parent category to see if there's a coupon for it. 
    //If so, return the coupon name. Repeat this process until there is no parent category, then return null.
    public static String findCoupon(String givenCategory, Coupon[] coupons, Category[] categories)
    {
        // category to coupon
        Map<String, List<String>> categoryToCoupon  = new HashMap<>();
        for(Coupon coupon : coupons)
        {
            categoryToCoupon.putIfAbsent(coupon.categoryName, new ArrayList<String>());
            categoryToCoupon.get(coupon.categoryName).add(coupon.couponName);
        }

        // category parents
        Map<String, String> categoryParent = new HashMap<>();
        for(Category category: categories)
        {
            categoryParent.put(category.categoryName, category.parentCategoryName);
        }

        while(!categoryToCoupon.containsKey(givenCategory))
        {
            if(categoryParent.containsKey(givenCategory))
            {
                givenCategory = categoryParent.get(givenCategory);
            }
            else
            {
                givenCategory = null;
                break;
            }
        }

        return givenCategory == null ? null : categoryToCoupon.get(givenCategory).get(0);
    }

    public static String findLatestCoupon(String givenCategory, CouponWithDate[] coupons, Category[] categories)
    {
        // category to coupon
        Map<String, PriorityQueue<CouponWithDate>> categoryToCoupon  = new HashMap<>();
        //SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");  
        for(CouponWithDate coupon : coupons)
        {
            categoryToCoupon.putIfAbsent(coupon.categoryName, new PriorityQueue<CouponWithDate>( (a, b) -> 
                                                    {return b.date.compareTo(a.date);}));
            categoryToCoupon.get(coupon.categoryName).offer(coupon);
        }

        // category parents
        Map<String, String> categoryParent = new HashMap<>();
        for(Category category: categories)
        {
            categoryParent.put(category.categoryName, category.parentCategoryName);
        }

        while(!categoryToCoupon.containsKey(givenCategory))
        {
            if(categoryParent.containsKey(givenCategory))
            {
                givenCategory = categoryParent.get(givenCategory);
            }
            else
            {
                givenCategory = null;
                break;
            }
        }

        return givenCategory == null ? null : categoryToCoupon.get(givenCategory).peek().couponName;
    }
    
}

class Coupon
{
    String couponName;
    String categoryName;

    public Coupon(String coupon, String category)
    {
        this.couponName = coupon;
        this.categoryName = category;
    }
}

class Category
{
    String categoryName;
    String parentCategoryName;

    public Category(String category, String parentCategory)
    {
        this.categoryName = category;
        this.parentCategoryName = parentCategory;
    }
}

class CouponWithDate
{
    String couponName;
    String categoryName;
    Date date;
    
    public CouponWithDate(String coupon, String category, String date)
    {
        this.couponName = coupon;
        this.categoryName = category;

        try
        {
            SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");  
            this.date = formatter.parse(date);
        }
        catch(final ParseException ex)
        {
            System.err.println(ex);
        }
    }
}


```

example .\
List One. "Code1":"value1"\
"Code2":"value2\
"Code3": "Value3"\
"Code3": "ValueValue4"

List Two: "Customer1": "Code1"\
"Customer2": "Code1"\
"Customer3": "Code3"\
"Customer4": "Customer1"\
"Custimer5":"Customer6"

```
		input         output
Customer 1      Value1
Customer4.      Value1(same as customer 1)
Customer5.      null. ( not present in list1)
Customer3.     value3  ( decide between value 3 and value value 3 based on date added)
Customer2.      value1
Customer6.     null
```
