Implement calculator that evaluates string expressions.
Requirements:
function calculate(expression: string): number
Supported Operations:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
Constraints:
- Only integers in expression
- No parentheses
- Handle operator precedence
- Handle whitespace
Example:
Input: "3 + 2 * 2"
Output: 7
Implementation:
1. Parse expression
2. Handle operator precedence
3. Evaluate sub-expressions
4. Return final result
Design database system with basic CRUD operations.
Core Operations:
interface Database<K, V> {
// Required Operations
insert(key: K, value: V): void
remove(key: K): boolean
retrieve(key: K): V | null
// Optional Operations
listKeys(): K[]
update(key: K, value: V): boolean
}
Requirements:
1. Data Storage
- Efficient key-value storage
- Handle collisions
- Memory management
2. Operation Handling
- Fast lookups
- Safe deletions
- Atomic updates
3. Error Cases
- Key not found
- Duplicate keys
- Invalid operations
Find maximum minimum sweetness after dividing chocolate bar.
function maxMinSweetness(
sweetness: number[],
k: number
): number
Examples:
1. Input: sweetness = [1,2,3,4,5,6,7,8,9], k = 5
Output: 6
Splits: [1,2,3], [4,5], [6], [7], [8], [9]
2. Input: sweetness = [5,6,7,8,9,1,2,3,4], k = 8
Output: 1
Explanation: Only one way to split into 9 pieces
3. Input: sweetness = [1,2,2,1,2,2,1,2,2], k = 2
Output: 5
Splits: [1,2,2], [1,2,2], [1,2,2]
Requirements:
- k cuts create k+1 pieces
- Each piece has consecutive chunks
- Return max possible minimum sweetness
- Optimize for large inputs
Return balanced string with minimum deletions.
function minDeletionsToBalance(s: string): string
Requirements:
- Remove minimum characters
- Maintain proper ordering
- Match all parentheses
Example:
Input: "())"
Output: "()"
Input: "((("
Output: "()"
Rules:
1. Every '(' needs matching ')'
2. Proper nesting required
3. Minimize deletions
Approach:
1. Track opening/closing counts
2. Identify unmatched pairs
3. Remove minimum necessary
4. Maintain balance property