这次面试是 Meta 的技术轮。题目一出来,表面上不复杂,却有点“陷阱”:
原题
Given a string s consisting of lowercase English characters, determine if you can make it a palindrome by removing at most 1 letter.
Example:
- Input: s = "aba" → Output: true
- Input: s = "abca" → Output: true
- Input: s = "cupucup" → Output: true
- Input: s = "abcde" → Output: false
👀 面试当时的第一反应
我看到题的时候,心里在想:“这不就是一个变种的回文检测吗?”但立马又觉得不妙,因为如果光说“我用双指针扫一遍”太笼统,面试官一定会追问:“那遇到不匹配怎么办?”、“怎么保证只删一次?”、“复杂度是多少?”
🧩 辅助的过程
这个时候,辅助老师在后台给我快速打了一行提示:
“先用双指针从两边往中间比,如果遇到不等,尝试跳过左边或右边各一次,再判断剩下的子串是不是回文。”
然后又给我逐字稿式的解释:
- “So the core idea is using two pointers. If characters match, move inward. If they don’t match, we have two options: skip the left or skip the right, but only once. Then check if the rest is palindrome.”
- “The complexity is O(n), since each check is linear.”
我照着念出来,面试官明显露出了“嗯,挺清楚”的表情。
🖊 代码的关键片段
虽然我不用把整段代码背下来,但csoahelp辅助老师给我准备了一个关键骨架,让我能快速写:
def validPalindrome(s):
def is_pal(l, r):
while l < r:
if s[l] != s[r]:
return False
l, r = l+1, r-1
return True
l, r = 0, len(s)-1
while l < r:
if s[l] != s[r]:
return is_pal(l+1, r) or is_pal(l, r-1)
l, r = l+1, r-1
return True
我当场一边写一边解释,给了几个例子验证,逻辑就很顺畅。
💬 和面试官的互动
写完后,面试官追问:“What if the string is extremely long? Do you think your solution still works?”
我按照准备好的话说:“Yes, the time complexity is O(n), and we only use constant extra space. It’s efficient enough even for long strings.”
这一点正好戳中了考官想听的点。
🎯 最后的收获
说实话,如果没有csoahelp辅助老师的逐字稿,我可能会卡在“遇到不等时怎么处理”那里。因为这种题写出来容易,但解释清楚、保证正确性才是关键。
有了辅助,我不仅写对了,还能用地道的英语解释思路,整个过程显得很稳。
📌 总结给读者的建议
- 面试里别怕题目看似简单,往往考察的是你的表达和细节。
- 双指针、贪心、二分这些常用技巧,最好能用一句话说清楚原理。
- 最后记得给时间复杂度和空间复杂度,不然答案不完整。
如果你也在准备大厂的BQ, 算法与系统设计面试,欢迎添加微信,即可领取北美面试求职通关秘诀。我们也有代面试,面试辅助,OA代写等服务助您早日上岸~
