Day209 - Leetcode: Python 20 & MLOps Review: ML Engineering (1)
Python 20: Valid Parentheses & ML Engineering: High-level ML System Design

๐ฉ Python Review
20. Valid Parentheses
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
- Every close bracket has a corresponding open bracket of the same type.
Solution
Youโre given a string s of characters:
- Only
'(' , ')' , '{' , '}' , '[' , ']'.
Check if it is valid, meaning:
- Every open bracket must be closed by the same type.
- Brackets must close in the correct order (LIFO).
- No unmatched closing bracket.
Concepts
1. Naรฏve Approach
Try all (buy, sell) pairs โ 0(n^2) time. It is too slow for large inputs.
2. Key Observation
- Profit =
sell_price - buy_price. - To maximize profit, we need to buy the lowest before selling the largest.
- The minimum prices so far (best day to buy thus far).
- The maximum profit so far if it is sold today.
3. Greedy One-Pass Algorithm
Each day:
min_price = min(min_price, price[i])profit = price[i] - min_pricemax_profit = max(max_profit, profit)
Solution Code
from typing import List
class Solution:
def maxProfit(self, prices: List[int]) -> int:
min_price = float('inf')
max_profit = 0
for price in prices:
if price < min_price:
min_price = price # update buying day
else:
max_profit = max(max_profit, price - min_price)
return max_profit
- Only one pass is needed:
O(n)time,O(1)space. - Greedy works because the best profit depends solely on the lowest seen price before the current day.
๐ง MLOps Review

I will begin reviewing the book: Machine Learning Engineering with Python by Andrew P. McMahon. This practical guide helps turn machine learning projects into successful deployments by building and scaling solutions that address real-world problems. It also includes a new chapter on generative AI and large language models (LLMs), covering the building of a pipeline that utilizes LLMs with LangChain. I hope to gain hands-on knowledge through exercises in this book.
High-Level ML System Design
Typically, ML engineering in the real world encompasses a range of diverse structures and objectives. Letโs go through some examples where a team needs to design a high-level ML system for common business problems.
Example: Batch Anomaly Detection Service
Suppose you work for a taxi company with thousands of cars. The company aims to make ride times more consistent and analyze longer journeys to enhance customer experience, increase retention, and encourage repeat business. Your ML team is tasked with developing an anomaly detection service to spot rides with unusual duration or length behaviors. If the plot looks like this, what should we do?

We can agree that running this daily could yield enough data for profound insights. Then, how will you schedule that run? Here, you will need an orchestration layer, which is a tool or set of tools that enable you to plan and manage predefined jobs. A tool like Apache Airflow would do exactly like this.
What do you do next? Since runs are daily and data volume is high, leveraging distributed computing makes sense. Two options are familiar: Apache Spark and Ray. To minimize infrastructure dependencies and refactoring, you choose Ray. The dataโs endpoint is a SQL database table, so you coordinate with the database team to plan data handover. For security and reliability, direct writes to the production database are avoided. Instead, a cloud database serves as an intermediate staging area for daily queries.
The example flow highlights the ML design process. It may appear cluttered, but it is not. Letโs fill the gaps one step at a time.
Leave a comment