Baidu Coding Interview Questions
25 Baidu coding interview problems with full optimal solutions — 11 easy, 11 medium, 3 hard. Every problem ships with multiple approaches (brute-force first, then the optimal), complexity tables for each, company-specific tips on what an Baidu interviewer values, and a FAQ section.
Showing 25 problems of 25
- #1easyfoundational
1. Two Sum
Find two indices in an array whose values sum to a target.
- #2easyfoundational
2. Valid Parentheses
Determine if a string of brackets is properly opened and closed.
- #3easyfoundational
3. Merge Two Sorted Lists
Combine two sorted linked lists into one sorted list.
- #4easyfoundational
4. Remove Duplicates from Sorted Array
Remove duplicates from a sorted array in-place and return the new length.
- #5easyfoundational
5. Same Tree
Decide whether two binary trees are structurally identical and hold the same values at every node.
- #6easyfoundational
6. Maximum Depth of Binary Tree
Return the length of the longest root-to-leaf path in a binary tree.
- #7easyfoundational
7. Best Time to Buy and Sell Stock
Find the maximum profit from a single buy-then-sell over a daily price series.
- #8easyfoundational
8. Single Number
Find the one element in an array where every other element appears exactly twice.
- #9easyfoundational
9. Linked List Cycle
Detect whether a singly linked list contains a cycle, using only O(1) extra memory.
- #10easyfoundational
10. Majority Element
Find the element that appears more than n/2 times in an array of length n.
- #11easyfoundational
11. Reverse Linked List
Reverse a singly linked list in-place and return the new head.
- #12mediumfoundational
12. Binary Tree Level Order Traversal
Return the values of a binary tree visited level by level, left to right.
- #13mediumfoundational
13. LRU Cache
Design a fixed-capacity cache that evicts the least-recently-used key when full, supporting get and put in O(1).
- #14mediumfoundational
14. Number of Islands
Count connected components of land cells in a 2D grid where '1' is land and '0' is water.
- #15mediumfoundational
15. Course Schedule
Decide whether all courses can be finished given prerequisite pairs — equivalently, whether a directed graph is acyclic.
- #16mediumfoundational
16. Kth Largest Element in an Array
Find the kth largest element in an unsorted array without fully sorting it.
- #17mediumfoundational
17. Longest Increasing Subsequence
Find the length of the longest strictly increasing subsequence of an integer array.
- #18mediumfoundational
18. Coin Change
Find the fewest coins from a given set of denominations needed to make a target amount, or -1 if impossible.
- #19mediumfoundational
19. Subarray Sum Equals K
Count the number of contiguous subarrays whose elements sum exactly to k.
- #20mediumfoundational
20. Implement Trie (Prefix Tree)
Build a trie supporting insert, search, and startsWith on lowercase ASCII words.
- #21mediumfoundational
21. Find All Anagrams in a String
Return every start index in s where a substring of length |p| is an anagram of p.
- #22mediumfoundational
22. Daily Temperatures
For each day, report how many days you have to wait until a warmer temperature, or 0 if there is none.
- #23hardfoundational
23. Sliding Window Maximum
For each window of size k in an array, return the maximum value in linear total time.
- #24hardfoundational
24. Find Median from Data Stream
Design a class that ingests integers one at a time and returns the median of all values seen so far in O(log n).
- #25hardfoundational
25. LFU Cache
Design a fixed-capacity cache that evicts the least-frequently-used key (ties broken by least-recent) in O(1) per op.