LEETCODE
50 COMMON INTERVIEW QUESTIONS
Clean Code
Handbook
Edition
1
S
Sold to Di Liu using Selz (#TPBAJERE)
1
L E E T C O D E
Clean Code Handbook
2014 LeetCode
admin@leetcode.com
2
CHAPTER 0: FOREWORD ....................................................................................................................... 4
CHAPTER 1: ARRAY/STRING ............................................................................................................... 5
1. TWO SUM .................................................................................................................................................. 5
2. TWO SUM II – INPUT ARRAY IS SORTED ................................................................................................. 6
3. TWO SUM III – DATA STRUCTURE DESIGN ............................................................................................. 8
4. VALID PALINDROME .............................................................................................................................. 10
5. IMPLEMENT STRSTR() ........................................................................................................................... 11
6. REVERSE WORDS IN A STRING ............................................................................................................. 12
7. REVERSE WORDS IN A STRING II .......................................................................................................... 13
8. STRING TO INTEGER (ATOI) ................................................................................................................. 14
9. VALID NUMBER ...................................................................................................................................... 16
10. LONGEST SUBSTRING WITHOUT REPEATING CHARACTERS ........................................................... 19
11. LONGEST SUBSTRING WITH AT MOST TWO DISTINCT CHARACTERS ............................................ 21
12. MISSING RANGES ................................................................................................................................. 23
13. LONGEST PALINDROMIC SUBSTRING ................................................................................................. 24
14. ONE EDIT DISTANCE ........................................................................................................................... 27
15. READ N CHARACTERS GIVEN READ4 ................................................................................................ 29
16. READ N CHARACTERS GIVEN READ4 – CALL MULTIPLE TIMES ...................................................... 30
CHAPTER 2: MATH ............................................................................................................................... 32
17. REVERSE INTEGER .............................................................................................................................. 32
18. PLUS ONE ............................................................................................................................................. 34
19. PALINDROME NUMBER ....................................................................................................................... 35
CHAPTER 3: LINKED LIST .................................................................................................................. 37
20. MERGE TWO SORTED LISTS ............................................................................................................... 37
21. ADD TWO NUMBERS ........................................................................................................................... 38
22. SWAP NODES IN PAIRS ....................................................................................................................... 39
23. MERGE K SORTED LINKED LISTS ....................................................................................................... 40
24. COPY LIST WITH RANDOM POINTER ................................................................................................. 43
CHAPTER 4: BINARY TREE ................................................................................................................ 46
25. VALIDATE BINARY SEARCH TREE ...................................................................................................... 46
26. MAXIMUM DEPTH OF BINARY TREE ................................................................................................. 49
27. MINIMUM DEPTH OF BINARY TREE .................................................................................................. 50
28. BALANCED BINARY TREE ................................................................................................................... 52
29. CONVERT SORTED ARRAY TO BALANCED BINARY SEARCH TREE .................................................. 54
30. CONVERT SORTED LIST TO BALANCED BINARY SEARCH TREE ....................................................... 56
31. BINARY TREE MAXIMUM PATH SUM ................................................................................................. 58
32. BINARY TREE UPSIDE DOWN ............................................................................................................. 60
CHAPTER 5: BIT MANIPULATION ................................................................................................... 62
33. SINGLE NUMBER .................................................................................................................................. 62
34. SINGLE NUMBER II .............................................................................................................................. 64
CHAPTER 6: MISC .................................................................................................................................. 66
3
35. SPIRAL MATRIX ................................................................................................................................... 66
36. INTEGER TO ROMAN ........................................................................................................................... 68
37. ROMAN TO INTEGER ........................................................................................................................... 70
38. CLONE GRAPH ...................................................................................................................................... 72
CHAPTER 7: STACK .............................................................................................................................. 74
39. MIN STACK .......................................................................................................................................... 74
40. EVALUATE REVERSE POLISH NOTATION .......................................................................................... 76
41. VALID PARENTHESES .......................................................................................................................... 80
CHAPTER 8: DYNAMIC PROGRAMMING ........................................................................................ 81
42. CLIMBING STAIRS ................................................................................................................................ 81
43. UNIQUE PATHS .................................................................................................................................... 83
44. UNIQUE PATHS II ................................................................................................................................ 86
45. MAXIMUM SUM SUBARRAY ................................................................................................................ 87
46. MAXIMUM PRODUCT SUBARRAY ....................................................................................................... 89
47. COINS IN A LINE ................................................................................................................................... 90
CHAPTER 9: BINARY SEARCH ........................................................................................................... 95
48. SEARCH INSERT POSITION ................................................................................................................. 95
49. FIND MINIMUM IN SORTED ROTATED ARRAY .................................................................................. 97
50. FIND MINIMUM IN ROTATED SORTED ARRAY II – WITH DUPLICATES ........................................... 99
4
Chapter 0: Foreword
Hi, fellow LeetCoders:
First, I would like to express thank you for buying this eBook: “Clean Code Handbook:
50 Common Interview Questions”. As the title suggested, this is the definitive guide to
write clean and concise code for interview questions. You will learn how to write clean
code. Then, you will ace the coding interviews.
This eBook is written to serve as the perfect companion for LeetCode Online Judge (OJ).
Each problem has a “Code it now” link that redirects to the OJ problem statement page.
You can write the code and submit it to the OJ system, which you will get immediate
feedback on whether your solution is correct. If the “Code it now” link says “Coming
soon”, it means the problem will be added very soon in the future, so stay tuned.
On the top right side of each problem are the Difficulty and Frequency ratings. There are
three levels of difficulty: Easy, Medium, and Hard. Easy problems are problems that are
easy to come up with ideas and the implementation should be pretty straightforward.
Most interview questions fall into this level of difficulty.
On the other end, there are hard problems. Hard problems are usually algorithmic in
nature and require more thoughts beforehand. There could be some coding questions that
fall into Hard, but that is rare.
There are three frequency rating: Low, Medium, and High. The frequency of a problem
being asked in a real interview is based on data collected from the user survey: “Have you
met this question in a real interview?” This should give you an idea of what kind of
questions is currently being asked in real interviews.
Each question may contain a section called “Example Questions Candidate Might Ask”.
These are examples of good questions to ask your interviewer. Asking clarifying
questions is important and is a good chance to demonstrate your thought process.
Each question is accompanied with as many approaches as possible. Each approach
begins with a runtime and space complexity summary so you can quickly compare
between different approaches. The analysis of the runtime and space complexity is
usually provided along with the solution. Analyzing complexity is frequently being asked
in a technical interview, so you should definitely prepare for it.
You learn best when you solve a problem by yourself. If you get stuck, there are usually
hints in the book to help you. If you are still stuck, read the analysis and try to write the
code yourself in the Online Judge
Even though you might think a problem is easy, writing code that is concise and clean is
not as easy as most people think. For example, if you are writing more than 30 lines of
code during a coding interview, your code is probably not concise enough. Most code in
this eBook fall between 20 — 30 lines of code.
1337c0d3r
评论0
最新资源