Introduction
This section contains coding exercises commonly asked in technical interviews, based on real interview experiences from companies like Alibaba, Meituan, Pinduoduo, and ByteDance.For each problem, try to solve it yourself first before looking at the solution. Understanding the problem-solving process is more important than memorizing solutions.
String & Array Problems
Problem 1: Adjacent Character Substring (Aliyun)
Problem Description
Problem Description
Description:Given a string, find the number of continuous substrings where no two adjacent characters are the same.Input:Output:Explanation:
- First line: positive integer n (string length, max 200,000)
- Second line: string of lowercase letters
- Number of valid continuous substrings
- Empty string (length 0): 1
- Length 1: “b”, “e”, “e” → 3 valid
- Length 2: “be” → 1 valid (“ee” is invalid)
- Length 3: “bee” → 0 valid
- Total: 1 + 3 + 1 = 5
Solution Approach
Solution Approach
Strategy:
- Count substrings of each valid length
- For each position, find the longest valid substring starting there
- Sum all possible substrings
Problem 2: 01 String Alternating Pattern (Pinduoduo)
Problem Description
Problem Description
Description:Given a binary string (0s and 1s), you can perform the following operation any number of times:Output:Explanation: Split as “10” and “010”, reverse to get “01” and “010”, concatenate to “01010” (length 5)
- Split the string into two parts
- Reverse each part individually
- Concatenate them in the original order
Solution Approach
Solution Approach
Key Insight:The operation is equivalent to treating the string as a circular array (ring structure). Any split and reverse operation corresponds to selecting a substring from the doubled string.Strategy:Time Complexity: O(n)
Space Complexity: O(n) for the doubled string
- Double the string:
s = s + s(simulate circular structure) - Find the longest alternating 01 pattern in the doubled string
- Result cannot exceed original string length n
Mathematical & Logic Problems
Problem 3: GCD Prime Number (Meituan)
Problem Description
Problem Description
Description:Given an integer n > 1, find an integer m (2 ≤ m ≤ n) such that gcd(n, m) is a prime number.Input:Output:Explanation:
- First line: t (number of test cases, 1 ≤ t ≤ 100)
- For each test case: integer n (2 ≤ n ≤ 10⁵)
- For each test case: output m
- If multiple answers exist, output any valid one
- gcd(10, 2) = 2 (prime)
- gcd(13, 26) = 13 (prime)
Solution Approach
Solution Approach
Strategy:Time Complexity: O(n × √n) - checking each m and testing primality
Space Complexity: O(1)
- Try all values of m from 2 to n
- Calculate gcd(n, m)
- Check if gcd is prime
- Return first valid m
- Use Euclidean algorithm for GCD
- Optimize prime checking
Problem 4: Minimum Array Range (Meituan)
Problem Description
Problem Description
Description:Given an array of integers, you can perform the following operations:Output:Explanation: After 3 operations, array can become [2,2,3,4,4] or similar with minimum range.
- Decrease the maximum element by 1
- Increase the minimum element by 1
- First line: n (array length)
- Second line: n integers
- Minimum number of operations
Solution Approach
Solution Approach
Key Insight:After operations, the final array will have all elements in range [min_final, max_final]. The optimal strategy is to find which elements should be the boundary.Strategy:Time Complexity: O(n²)
Space Complexity: O(1) excluding input array
- Sort the array
- For each possible split point i:
- Elements before i will be increased to nums[i]
- Elements after i+1 will be decreased to nums[i+1]
- Calculate operations needed for each split
- Return minimum
Data Structure Problems
Problem 5: Graph Restoration
Problem Description
Problem Description
Description:Given n points and their shortest distances from point 1, restore a valid undirected graph where:Output:
- No node has degree > limit
- The shortest distances are preserved
- Line 1: n (nodes), limit (max degree)
- Line 2: n integers d_i (distance from node 1 to node i)
- First line: m (number of edges)
- Next m lines: u v (edge between u and v)
Solution Approach
Solution Approach
Strategy:Time Complexity: O(n × limit)
Space Complexity: O(n)
- Group nodes by their distance from node 1
- Connect each node to a node in the previous distance level
- Ensure no node exceeds degree limit
Classic Algorithm Problems
Sorting Algorithms Reference
Quick Sort Implementation
Quick Sort Implementation
Merge Sort Implementation
Merge Sort Implementation
Binary Search Implementation
Binary Search Implementation
Common Data Structures
Array and List Operations
Problem-Solving Tips
Clarify Requirements
- Ask about input constraints
- Clarify edge cases
- Confirm expected output format
- Understand performance requirements
Choose Right Approach
- Brute force first, then optimize
- Consider time/space trade-offs
- Identify patterns (two pointers, sliding window, etc.)
- Use appropriate data structures
Test Your Code
- Test with example cases
- Test edge cases (empty, single element, max size)
- Test with negative numbers if applicable
- Verify time complexity
Communicate Clearly
- Explain your thought process
- Discuss trade-offs
- Mention alternative approaches
- Ask for feedback
Practice Resources
Recommended Platforms:- LeetCode: Most popular, excellent for FAANG preparation
- 牛客网 (Nowcoder): Chinese companies’ actual interview problems
- 力扣 (LeetCode CN): Chinese version with local company problems
- HackerRank: Good for fundamentals and company assessments
- Arrays and Strings (2 weeks)
- Linked Lists (1 week)
- Stacks and Queues (1 week)
- Trees and Graphs (2 weeks)
- Dynamic Programming (2 weeks)
- Advanced Topics (2 weeks)
Back to Common Questions
Review interview Q&A before applying your coding skills