1. [OA] Sliding Window — Longest Substring with K Distinct Characters in ByteDance’s social media feed
ByteDance's social media feed often presents users with posts that have various tags. Implement an algorithm that finds the longest substring containing at most k distinct characters. Problem Statement: Write a function longestSubstring(s: str, k: int) -> int that returns the length of the longest substring containing at most k distinct characters. - Example 1: Input: longestSubstring("eceba", 2) Output: 3 Explanation: The longest substring is "ece". - Example 2: Input: longestSubstring("aa", 1) Output: 2 Explanation: The longest substring is "aa". Constraints: - 1 <= s.length <= 10^5 - 0 <= k <= |s|
codingHarddynamic programming#2
2. [OA] Dynamic Programming — Maximum Product Subarray in ByteDance’s e-commerce catalog
In ByteDance's e-commerce app, we analyze product performances using their price impact over periods. Calculate the maximum product of a contiguous subarray of product prices. Problem Statement: Write a function maxProduct(nums: List[int]) -> int that returns the maximum product of a contiguous subarray in the given integer array nums. - Example 1: Input: maxProduct([2, 3, -2, 4]) Output: 6 Explanation: The maximum product is 2 * 3 = 6. - Example 2: Input: maxProduct([-2, 0, -1]) Output: 0 Explanation: The maximum product is 0. Constraints: - 1 <= nums.length <= 2 * 10^4 - -100 <= nums[i] <= 100