nirajs

wayfair coding

Feb 5th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.20 KB | None | 0 0
  1.  
  2. // Coupons = [
  3. //     {"CategoryName":"Comforter Sets", "CouponName":"Comforters Sale"},
  4. //     {"CategoryName":"Bedding", "CouponName":"Savings on Bedding"},
  5. //     {"CategoryName":"Bed & Bath", "CouponName":"Low price for Bed & Bath"}
  6. // ]
  7.  
  8. // Categories = [
  9. //     {"CategoryName":"Comforter Sets", "CategoryParentName":"Bedding"},
  10. //     {"CategoryName":"Bedding", "CategoryParentName":"Bed & Bath"},
  11. //     {"CategoryName":"Bed & Bath", "CategoryParentName":null},
  12. //     {"CategoryName":"Soap Dispensers", "CategoryParentName":"Bathroom Accessories"},
  13. //     {"CategoryName":"Bathroom Accessories", "CategoryParentName":"Bed & Bath"},
  14. //     {"CategoryName":"Toy Organizers", "CategoryParentName":"Baby And Kids"},
  15. //     {"CategoryName":"Baby And Kids", "CategoryParentName":null}
  16. // ]
  17.  
  18. // # tests: input(CategoryName) => output(CouponName)
  19.  
  20. // "Comforter Sets"       => "Comforters Sale"
  21. // "Bedding"              => "Savings on Bedding"
  22. // "Bathroom Accessories" => "Low price for Bed & Bath"
  23. // "Soap Dispensers"      => "Low price for Bed & Bath"
  24. // "Toy Organizers"       => null
  25.  
  26.  
  27. /*
  28.  
  29.  
  30. child -> parent
  31.  
  32.      a ->  [c,d,e]
  33.     /  \
  34.    c [e,f]   d
  35.   /  \
  36.  e    f
  37.  
  38.  
  39.  Meta Data  (Table)
  40.  a -> [c,d]
  41.  c -> [e,f]
  42.  
  43.  
  44.  
  45.     a
  46.    /
  47.   e   c   d
  48. p[a] = null
  49. p[c] = a
  50. p[d] = a
  51. p[e] = c
  52.  
  53.  
  54. coupon(e) -> coupon(c)
  55.  
  56.  
  57.  
  58. Coupons (cat) -> coupon
  59. tree out of category
  60.     parent[CategoryName] -> CategoryName
  61.  
  62.  
  63.  
  64. input
  65.  
  66.  
  67. 1 query
  68. Time = height of tree  (n)
  69.  
  70.  
  71. Coupons
  72.     {"CategoryName":"Comforter Sets", "CouponName":"Comforters Sale"},
  73.  
  74. Categories
  75. // Categories = [
  76. //     {"CategoryName":"Comforter Sets", "CategoryParentName":"Bedding"},
  77.  
  78.  */
  79.  
  80.  
  81. import java.util.HashMap;
  82. import java.util.List;
  83. import java.util.Map;
  84.  
  85. public class Main {
  86.  
  87.  
  88.     static Map<String, String> couponMap = new HashMap<>();
  89.     static Map<String, String> categoryParent = new HashMap<>();
  90.     static Map<String, String> categoryToCoupon = new HashMap<>();
  91.  
  92.     static String processGetCouponFromCategory(String category) {
  93.         if (category == null) {
  94.             return null;
  95.         }
  96.         if (couponMap.get(category) != null) {
  97.             return couponMap.get(category);
  98.         }
  99.         else {
  100.             return processGetCouponFromCategory(categoryParent.get(category));
  101.         }
  102.     }
  103.  
  104.     void preProcessAllCategory(List<String> categoryList) {
  105.         for (int i=0; i<categoryList.size(); i++) {
  106.             String cat = categoryList.get(i);
  107.             categoryToCoupon.put(cat, processGetCouponFromCategory(cat));
  108.         }
  109.         return ;
  110.     }
  111.  
  112.     String getCoupon(String category) {
  113.         if (categoryToCoupon.get(category) != null) {
  114.             return categoryToCoupon.get(category);
  115.         }
  116.         return null;
  117.     }
  118.  
  119.  
  120.  
  121.     public static void main(String[] args) {
  122.  
  123.  
  124.         // Coupons = [
  125. //     {"CategoryName":"Comforter Sets", "CouponName":"Comforters Sale"},
  126. //     {"CategoryName":"Bedding", "CouponName":"Savings on Bedding"},
  127. //     {"CategoryName":"Bed & Bath", "CouponName":"Low price for Bed & Bath"}
  128. // ]
  129.  
  130.         couponMap.put("Comforter Sets", "Comforters Sale");
  131.         couponMap.put("Bedding", "Savings on Bedding");
  132.         couponMap.put("Bed & Bath", "Low price for Bed & Bath");
  133.  
  134.  
  135.         // Categories = [
  136. //     {"CategoryName":"Comforter Sets", "CategoryParentName":"Bedding"},
  137. //     {"CategoryName":"Bedding", "CategoryParentName":"Bed & Bath"},
  138. //     {"CategoryName":"Bed & Bath", "CategoryParentName":null},
  139. //     {"CategoryName":"Soap Dispensers", "CategoryParentName":"Bathroom Accessories"},
  140. //      {"CategoryName":"Bathroom Accessories", "CategoryParentName":"Bed & Bath"},
  141. //     {"CategoryName":"Toy Organizers", "CategoryParentName":"Baby And Kids"},
  142. //     {"CategoryName":"Baby And Kids", "CategoryParentName":null}
  143. // ]
  144.  
  145.         categoryParent.put("Comforter Sets", "Bedding");
  146.         categoryParent.put("Bedding", "Bed & Bath");
  147.         categoryParent.put("Bed & Bath", null);
  148.         categoryParent.put("Soap Dispensers", "Bathroom Accessories");
  149.         categoryParent.put("Bathroom Accessories", "Bed & Bath");
  150.         categoryParent.put("Toy Organizers", "Baby And Kids");
  151.         categoryParent.put("Baby And Kids", null);
  152.  
  153.  
  154.         // # tests: input(CategoryName) => output(CouponName)
  155.  
  156. // "Comforter Sets"       => "Comforters Sale"
  157. // "Bedding"              => "Savings on Bedding"
  158. // "Bathroom Accessories" => "Low price for Bed & Bath"
  159. // "Soap Dispensers"      => "Low price for Bed & Bath"
  160. // "Toy Organizers"       => null
  161.  
  162.  
  163.  
  164.         String coupon = processGetCouponFromCategory("Toy Organizers");
  165.         System.out.println(coupon);
  166.  
  167.  
  168.  
  169.  
  170.  
  171. //        // Press Opt+Enter with your caret at the highlighted text to see how
  172. //
  173. //        // IntelliJ IDEA suggests fixing it.
  174. //        System.out.printf("Hello and welcome!");
  175. //
  176. //        // Press Ctrl+R or click the green arrow button in the gutter to run the code.
  177. //        for (int i = 1; i <= 5; i++) {
  178. //
  179. //            // Press Ctrl+D to start debugging your code. We have set one breakpoint
  180. //            // for you, but you can always add more by pressing Cmd+F8.
  181. //            System.out.println("i = " + i);
  182. //        }
  183.     }
  184. }
Add Comment
Please, Sign In to add comment