State Machine
state-machine
Practice Problems
Best Time to Buy and Sell Stock III
Find the maximum profit from at most two stock transactions, where you must sell before buying again.
Best Time to Buy and Sell Stock IV
Find the maximum profit from at most k stock transactions, generalizing the Stock III problem to arbitrary k.
Best Time to Buy and Sell Stock with Cooldown
Given an array of stock prices, find the maximum profit with unlimited transactions but a mandatory one-day cooldown after each sell.
String to Integer (atoi)
Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer. Handle leading whitespace, optional sign, digit parsing, and integer overflow/underflow.
Code Snippets
Generators as State Machines
Generators turn an explicit `switch (state)` block into a function whose own pause points encode the state. This snippet shows three flavors: a finite-state machine driven by `next(event)` for transitions, a step-by-step async sequencer that pauses between phases, and a richer machine with entry/exit side effects plus an unexpected-event handler. Reach for this when your control flow has a small, explicit set of states and you want the language to enforce them for you.
Question Banks
Interval and State-Machine DP
Interval DP (matrix chain, burst balloons shape) and state-machine DP (stock trading variants). Code stems are Python.
React State Management Without Redux Quiz
Four checks on the non-Redux options for sharing React state: local component state, Context, useReducer, and lightweight stores like Zustand or Jotai.
Community
The mapStateToProps / mapDispatchToProps Cheatsheet I Wish I Had In 2018
Every React + Redux codebase from before hooks revolves around connect. Three accordions of the wiring I keep paged in for inheriting one of those repos.
State Management: Zustand, Redux, and Context Compared
Three options, three sweet spots. Zustand is my default for client state, Context for low-frequency tree-wide values, Redux only for the cases that earn it.
Mobile Lifecycle and State Restoration Quiz
Four Android-flavored questions on activity lifecycle, configuration changes, and saved-state restoration. Aimed at native Android candidates and at backend devs interviewing onto a mobile-leaning team.
