计算机网络
- HTTP 状态码 301 和 302 的区别?如何处理 301 和 302?
- HTTP 2 和 HTTP 1.1 的区别
操作系统
数据库
-
隔离级别
-
MySQL 存储引擎 InnoDB 和 MyISAM 对比
InnoDB MyISAM 外键 支持 不支持 事务 支持 不支持 行表锁 行锁,操作时只锁某一行,适合高并发的操作 表锁,即使操作一条记录也会锁住整个表,不适合高并发的操作 缓存 不仅缓存索引还要缓存真实数据 只缓存索引,不缓存真实数据 表空间 小 大 -
B+ 树链表的作用
在范围查找时效率比 B 树高,因为 B+ 树的叶子节点通过有序链表连接,而 B 树要实现范围查找需要通过中序遍历才能完成。
-
MySQL 索引
- 索引(Index)时帮助 MySQL 高效获取数据的数据结构。
- 索引的目的在于提高查询效率
- 索引是在存储引擎(storage engine)层面实现的
程序语言
C++
- unordered_map 和 unordered_set 如何解决 hash 冲突
- vector 扩容和缩容的实现
Golang
- channel 的底层实现
- map 的底层实现
- context 相关
数据结构与算法
1. 堆排序 (Golang container/heap.go 实现)
package heap
import "sort"
type Interface interface {
sort.Interface
Push(x interface{}) // add x as element Len()
Pop() interface{} // remove and return element Len() - 1.
}
func Init(h Interface) {
// heapify
n := h.Len()
for i := n/2 - 1; i >= 0; i-- {
down(h, i, n)
}
}
func Push(h Interface, x interface{}) {
h.Push(x)
up(h, h.Len()-1)
}
func Pop(h Interface) interface{} {
n := h.Len() - 1
h.Swap(0, n)
down(h, 0, n)
return h.Pop()
}
func up(h Interface, j int) {
for {
i := (j - 1) / 2 // parent
if i == j || !h.Less(j, i) {
break
}
h.Swap(i, j)
j = i
}
}
func down(h Interface, i0, n int) bool {
i := i0
for {
j1 := 2*i + 1
if j1 >= n || j1 < 0 { // j1 < 0 after int overflow
break
}
j := j1 // left child
if j2 := j1 + 1; j2 < n && h.Less(j2, j1) {
j = j2 // = 2*i + 2 // right child
}
if !h.Less(j, i) {
break
}
h.Swap(i, j)
i = j
}
return i > i0
}
// Remove removes and returns the element at index i from the heap.
// The complexity is O(log n) where n = h.Len().
func Remove(h Interface, i int) interface{} {
n := h.Len() - 1
if n != i {
h.Swap(i, n)
if !down(h, i, n) {
up(h, i)
}
}
return h.Pop()
}
// Fix re-establishes the heap ordering after the element at index i has changed its value.
// Changing the value of the element at index i and then calling Fix is equivalent to,
// but less expensive than, calling Remove(h, i) followed by a Push of the new value.
// The complexity is O(log n) where n = h.Len().
func Fix(h Interface, i int) {
if !down(h, i, h.Len()) {
up(h, i)
}
}
对 int 型 slice 建小根堆示例:
type IntHeap []int
func (h IntHeap) Len() int { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) {
// Push and Pop use pointer receivers because they modify the slice's length,
// not just its contents.
*h = append(*h, x.(int))
}
func (h *IntHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[:n-1]
return x
}
func Example_intHeap() {
h := &IntHeap{2, 1, 5}
heap.Init(h)
heap.Push(h, 3)
fmt.Printf("minimum: %d\n", (*h)[0])
for h.Len() > 0 {
fmt.Printf("%d ", heap.Pop(h))
}
// Output:
// minimum: 1
// 1 2 3 5
}