Question 1: Search Suggestions System
Problem:
You are given a list of products, where each product is a string. You are also given a searchWord. After each character typed, return the top k suggestions of product names that match the typed prefix.
Each product also has an associated popularity score (Map<String, Integer>). Suggestions should be returned in order of:
Highest popularity score
If scores are equal, return the lexicographically smaller product.
You must return suggestions after each character of searchWord.
Handle up to 1e5 products and optimize for performance.
Test Case:
products = ["apple", "appetizer", "application", "app", "apply", "banana", "appstore"]
popularity = {
"apple": 80,
"appetizer": 70,
"application": 90,
"app": 90,
"apply": 85,
"banana": 60,
"appstore": 90
}
searchWord = "app"
k = 3
Expected Output:
[
["app", "application", "appstore"], # 'a'
["app", "application", "appstore"], # 'ap'
["app", "application", "appstore"] # 'app'
]
Question 2: Minimum Flips to Alternate Binary String (With K Flip Window)
Problem:
You are given a binary string s. In one operation, you can flip a subarray of length exactly k (flip all bits in that subarray). Return the minimum number of such operations needed to make the string alternating (i.e., no two adjacent bits are the same). If it's not possible, return -1.
Test Case: s = "00010111"
k = 3
Expected Output : 2
感谢分享!