想象你在 Meta 工作,每天有 5亿条 帖子、图片、视频被上传。你需要构建一个系统,在内容展示给用户之前,自动检测并过滤违规内容(仇恨言论、色情、暴力、虚假信息)。
You're at Meta. 500M pieces of content uploaded daily. Build a system that detects and filters violating content before it reaches users — hate speech, nudity, violence, misinformation.
from typing import Optional
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def reorderList(head: Optional[ListNode]) -> None:
ifnot head ornot head.next:
return# Step 1: Find middle using fast-slow pointers
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
# slow is now at the middle# Step 2: Reverse the second half
prev, curr = None, slow.next
slow.next = None# Cut the list in halfwhile curr:
nxt = curr.next
curr.next = prev
prev = curr
curr = nxt
# prev is now the head of reversed second half# Step 3: Merge two halves by interleaving
first, second = head, prev
while second:
tmp1, tmp2 = first.next, second.next
first.next = second
second.next = tmp1
first = tmp1
second = tmp2
**执行追踪 / Trace:** `1→2→3→4→5`
After Step 1: slow=3, list: 1→2→3 | 4→5
After Step 2: second half reversed: 5→4
After Step 3 (iter 1): 1→5→2→... second=4
After Step 3 (iter 2): 1→5→2→4→3
Result: 1→5→2→4→3 ✓
复杂度 / Complexity: Time O(n) · Space O(1)
与同模式题目的对比 / Variations in this Pattern
题目
用到的技巧
#206 Reverse Linked List
反转
#21 Merge Two Sorted Lists
合并
#141 Linked List Cycle
快慢指针(环检测)
#143 Reorder List ← 今天
找中点 + 反转 + 合并 (三合一)
#19 Remove Nth From End
快慢指针(间距k)
今天的关键洞察: Reorder List 是链表模式的"Boss题"——它不引入新技巧,而是要求你在一题中正确地组合三个已知工具,且顺序和边界条件不能出错。
"Tell me about a time you had to make a decision that was right for the company but unpopular with your team"
为什么这道题重要 / Why This Matters
Staff/Principal 工程师必须做的一件事:代表公司,而不只是代表团队。
面试官想看的是:你能否在团队不满的情况下,仍然清晰地论证和执行正确的决定——然后重建信任。
This question separates Staff+ engineers from Senior ones. It's not about being a pushover or a dictator — it's about principled decision-making under social pressure and bringing people along afterward.
You're building a SaaS dashboard. Protect all /dashboard/* routes — redirect unauthenticated users to /login. Must run at the Edge for low latency.
Next.js Middleware 核心概念 / Core Concept
Middleware 在请求到达页面之前在 Edge 运行,可以:
重定向 / Rewrite URL
修改请求/响应 headers
读取 Cookie(做认证检查)
Middleware runs before the page renders, at the Edge (Cloudflare Workers/Vercel Edge) — no cold starts, ~1ms overhead.
代码示例 / Code Snippet
// middleware.ts (root of project, NOT inside /app or /pages)
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
// Read session token from cookie (JWT or session cookie)
const token = request.cookies.get('session-token')?.value
// Protected route check
const isProtectedRoute = pathname.startsWith('/dashboard') ||
pathname.startsWith('/settings') ||
pathname.startsWith('/api/user')
if (isProtectedRoute && !token) {
// Redirect to login, preserve original URL for post-login redirect
const loginUrl = new URL('/login', request.url)
loginUrl.searchParams.set('from', pathname)
return NextResponse.redirect(loginUrl)
}
// Already logged in, trying to access /login → redirect to dashboard
if (token && pathname === '/login') {
return NextResponse.redirect(new URL('/dashboard', request.url))
}
return NextResponse.next()
}
// IMPORTANT: Control which paths trigger this middleware
export const config = {
matcher: [
// Match all except static files, images, favicon
'/((?!_next/static|_next/image|favicon.ico|public).*)',
],
}
猜猜这段代码的行为 / Quiz
// User visits /dashboard/settings without a session cookie
// What happens?
const from = '/dashboard/settings'
// loginUrl becomes: /login?from=%2Fdashboard%2Fsettings
Middleware 重定向并把原始路径编码到 from 参数。登录成功后可读取 from 参数并重定向回用户原本要访问的页面。
</details>
❌ 常见错误 vs ✅ 正确做法
❌ 错误:在 Middleware 里做数据库查询
// DON'T: DB calls are expensive and Middleware runs on every request
const user = await db.findUser(token) // ❌ Slow, can't use Node.js DB clients at Edge
✅ 正确:只做轻量 JWT 验证
// DO: Verify JWT signature (CPU-only, ~0.1ms)
import { jwtVerify } from 'jose'
const { payload } = await jwtVerify(token, secret)
// Full user data fetch happens in the page/API route itself
❌ 错误:把所有路由都加进 matcher
matcher: ['/:path*'] // ❌ Runs on static assets too, wasteful
Think of it like compressing a 4K photo to a thumbnail. Quantization takes a neural network's weights from 32-bit floats (float32) down to 8-bit integers (int8) or even 4-bit (int4). The model gets ~4x smaller with minimal quality loss.