Solve grid min path and sqrt by binary search
Company: TikTok
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in dynamic programming and algorithm design for grid-based path optimization as well as numerical methods and binary search for square-root approximation, encompassing time/space complexity analysis, external-memory handling for very large matrices, overflow avoidance, and numerical stability.
Minimum-Sum Grid Path
Constraints
- Cell values are non-negative
Examples
Input: ([[1, 3, 1], [1, 5, 1], [4, 2, 1]],)
Expected Output: [7, [[0, 0], [0, 1], [0, 2], [1, 2], [2, 2]]]
Input: ([[5]],)
Expected Output: [5, [[0, 0]]]
Input: ([[1, 1], [1, 1]],)
Expected Output: [3, [[0, 0], [0, 1], [1, 1]]]
Hints
- Fill DP from bottom-right and reconstruct greedily.
Integer Floor Square Root
Constraints
- x is non-negative
Examples
Input: (0,)
Expected Output: 0
Input: (1,)
Expected Output: 1
Input: (8,)
Expected Output: 2
Input: (16,)
Expected Output: 4
Input: (1000000000000,)
Expected Output: 1000000
Hints
- Binary search on the answer and compare with division to avoid overflow.
Real Square Root Approximation
Constraints
- x is non-negative
Examples
Input: (0.0,)
Expected Output: 0.0
Input: (2.0,)
Expected Output: 1.414214
Input: (0.25,)
Expected Output: 0.5
Input: (100.0,)
Expected Output: 10.0
Hints
- For 0 < x < 1, search in [0,1].