X7_OOD

§Design Food Delivery App

  • OOD fundamental concepts: Inheritance, encapsulation, abstrction and polymorphism
    First POJO(Plain Old Java Object), Then methods

  • Scenario, Use Cases

    1. User can search different restaurant
    2. User can select a restaurant
    3. User sees a menu
    4. Restaurant can change the menu any time
    5. User adds an item from menu
    6. User orders the food
    7. User can track the order in real time
    8. User can cancel the order
    9. User pays for the order
  • Solution

    Design Patterns involved in the design of this app:

    1. Builder Design Pattern (For adding food item and ordering)
    2. Iterator Pattern (User Sees Menu)
    3. Observer Pattern (Track an order in Real Time)
    4. (Command Design Pattern) Order and cancellation of Food
  • Major Classes

    User

    Restaurant

    Menu

    Item

    Order

  • Patterns

    • Builder Pattern

      reference: I, II

    • Iterator Pattern

    • Observer Pattern
      Subject —— notify() ——> Observers
      ​ <— subscribe(), unsubscribe() —
      eg. When DeliveryStatus changes, we can use observery pattern to notify users

      kafka:queue, pub/sub

    • Command Pattern
      eg. In order to pulic class CancelFood: IFoodOrderCommands and public class OrderFood: IFoodOrderCommands, so we can use command pattern: public interface IFoodOrderCommands

      java: runnable, callable

  • Summary

    1
    2
    3
    4
    class{ /*write class signature first,*/
    //implement
    //call servers
    }

§Reading Materials

  1. OODmock interview: Design a Vending Machine
  2. OODmock interview:How to design meeting room schedulingsystem
  3. Amazon Interview Question for SDE-2s | Design OO food delivery app catering to use cases
  4. Design OO food delivery app with C# & Design Patterns

§Problem

§760. Find Anagram Mappings | unordered_map
§Longest Common Substring | DP
§718. Maximum Length of Repeated Subarray | DP

double for loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (A[i-1] == B[j-1]){
dp[i][j] = 1 + dp[i-1][j-1];
update max;
}
//Space improvement
// Since we need f[i-1][j-1], we have to update from right
if (n > m)
return findLength(B,A);
vector<int> dp(n+1,0);
for (int i = 1; i <= m; ++i)
for (int j = n; j >= 1; --j)
if (A[i-1] == B[j-1]){
dp[j] = 1+dp[j-1];
res = max(res, dp[j]);
}
else
dp[j] = 0;
§ Bloomberg 高频题 | 航班规划 | Greedy

给你一堆人从纽约飞各个地方开会的 cost。比如
A 去 城市SF, LA的 cost 分别是 200, 300
B 去城市SF, LA的 cost 分别是 200, 400
C 去城市SF, LA的 cost 分别是 100, 400
D 去城市SF, LA的 cost 分别是 320, 210
然后保证一半的人去 SF,一半的人去 LA,使得总的 cost 最小

  1. Represent the Input: Two Dimensional Array / Matrix

  2. Greedy => number not equal
    if constSF>constLA, numLA++
    else if constLA>constSF, numSF++
    else numSame++ // Equal cost

  3. Switch smallest difference

  4. Check Legal

    1
    2
    3
    4
    if (arr = null || arr.length == 0)
    throw new IllegalArgumentException();
    if (m%2 != 0)
    throw new IllegalArgumentException();

    Or use Guava: Preconditions.checkArgument