Skip to content

Commit 5d7ba26

Browse files
Random stuff
1 parent d64467b commit 5d7ba26

File tree

6 files changed

+223
-7
lines changed

6 files changed

+223
-7
lines changed

Data_Struct_Implementation/endianessSwap/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,33 @@ int main (int argc, char *argv[]) {
3939
return 0;
4040
}
4141

42+
class Solution {
43+
public:
44+
uint32_t reverseBits(uint32_t n) {
45+
n = (n << 16) | (n >> 16);
46+
n = ((n << 8) & 0xff00ff00) | ((n >> 8) & 0x00ff00ff);
47+
n = ((n << 4) & 0xf0f0f0f0) | ((n >> 4) & 0x0f0f0f0f);
48+
n = ((n << 2) & 0xcccccccc) | ((n >> 2) & 0x33333333);
49+
n = ((n << 1) & 0xaaaaaaaa) | ((n >> 1) & 0x55555555);
50+
51+
return n;
52+
}
53+
};
54+
class Solution {
55+
public:
56+
uint32_t reverseBits(uint32_t n) {
57+
uint32_t ret{};
58+
int m = 32;
59+
60+
while(m --) {
61+
ret <<= 1;
62+
ret |= (n & 0x1);
63+
n >>= 1;
64+
}
65+
66+
return ret;
67+
}
68+
};
4269
```
4370
4471
## Reference

Interview/Company/facebook/algorithm_prepare.md

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,18 @@
3737
5. Add two number II Medium
3838
6. Linked List Insertion Sort Medium
3939
7. Swap Nodes in Pairs Medium
40+
8. Odd Even Linked List Medium
41+
9. Insert into a Cyclic Sorted List Medium
42+
10. Rotate List Medium
4043

4144
## String
4245
1. Valid Anagram Easy
4346
2. Integer to English Words Hard
4447

48+
## Bits Manipulation
49+
50+
## Data Structure
51+
1. LRU cache
4552

4653
## Implementation
4754

@@ -1472,4 +1479,182 @@ public:
14721479
return sentinal.next;
14731480
}
14741481
};
1482+
```
1483+
### **Odd Even Linked List**
1484+
1485+
***Big O:*** O(N) speed, O(1) space
1486+
```
1487+
Tips:
1488+
1489+
Construct the even and odd linked list first. Then hook the even list to the tail of the odd list.
1490+
```
1491+
```c++
1492+
class Solution {
1493+
public:
1494+
ListNode * oddEvenList(ListNode * head) {
1495+
// write your code here
1496+
if (!head || !head->next)
1497+
return head;
1498+
1499+
ListNode odd, even, *iterator, *odd_it, *even_it;
1500+
iterator = head;
1501+
odd_it = &odd;
1502+
even_it = &even;
1503+
1504+
int count = 1;
1505+
while (iterator) {
1506+
if (count % 2 != 0) {
1507+
odd_it->next = iterator;
1508+
odd_it = odd_it->next;
1509+
}
1510+
else {
1511+
even_it->next = iterator;
1512+
even_it = even_it->next;
1513+
}
1514+
count ++;
1515+
iterator = iterator->next;
1516+
}
1517+
even_it->next = nullptr;
1518+
odd_it->next = even.next;
1519+
1520+
return odd.next;
1521+
}
1522+
};
1523+
```
1524+
1525+
### **Insert into a Cyclic Sorted List**
1526+
1527+
***Big O:*** O(N) speed, O(1) space
1528+
```
1529+
Tips:
1530+
1531+
Pre and cur node for single linked list. Watch out for corner cases.
1532+
```
1533+
```c++
1534+
class Solution {
1535+
public:
1536+
ListNode * insert(ListNode * head, int x) {
1537+
// write your code here
1538+
if (!head) {
1539+
ListNode *new_node = new ListNode(x, nullptr);
1540+
new_node->next = new_node;
1541+
return new_node;
1542+
}
1543+
1544+
ListNode *pre = nullptr, *cur = head;
1545+
do {
1546+
pre = cur;
1547+
cur = cur->next;
1548+
1549+
if (pre->val <= x && cur->val >= x)
1550+
break;
1551+
if (pre->val > cur->val && (x > pre->val || x < cur->val))
1552+
break;
1553+
} while (cur != head);
1554+
1555+
ListNode *new_node = new ListNode(x, cur);
1556+
pre->next = new_node;
1557+
1558+
return head;
1559+
}
1560+
};
1561+
```
1562+
### **Rotate List**
1563+
1564+
***Big O:*** O(N) speed, O(1) space
1565+
```
1566+
Tips:
1567+
1568+
Count the nodes and count the effective move. Then rotate starting from the last node. Be careful of step = 1 case.
1569+
```
1570+
```c++
1571+
class Solution {
1572+
public:
1573+
ListNode * rotateRight(ListNode * head, int k) {
1574+
// write your code here
1575+
if (!head || !head->next)
1576+
return head;
1577+
1578+
int nodes_num = 1;
1579+
ListNode* iterator = head;
1580+
while (iterator->next) {
1581+
nodes_num ++;
1582+
iterator = iterator->next;
1583+
}
1584+
int effective_move = k%nodes_num;
1585+
if (effective_move == 0) return head;
1586+
1587+
iterator->next = head;
1588+
int step = nodes_num - effective_move;
1589+
while (step--) {
1590+
iterator = iterator->next;
1591+
}
1592+
1593+
ListNode *new_head = iterator->next;
1594+
iterator->next = nullptr;
1595+
1596+
return new_head;
1597+
}
1598+
};
1599+
```
1600+
1601+
### **LRU cache**
1602+
1603+
***Big O:*** O(1) speed, O(n) space
1604+
```
1605+
Tips:
1606+
1607+
Double linked list + hashmap
1608+
1609+
Use stl list to store the data and unordered hash map to record the position of the nodes.
1610+
```
1611+
```c++
1612+
#include <list>
1613+
1614+
class LRUCache {
1615+
public:
1616+
struct LRUNode {
1617+
int value;
1618+
int key;
1619+
1620+
LRUNode(int k, int v) : key(k), value(v) {}
1621+
};
1622+
1623+
LRUCache(int capacity) : cap(capacity) {}
1624+
1625+
int get(int key) {
1626+
if (!kv.count(key)) {
1627+
return -1;
1628+
}
1629+
move_front(key);
1630+
return values.front().value;
1631+
}
1632+
1633+
void set(int key, int value) {
1634+
if (!kv.count(key)) {
1635+
values.emplace_front(key, value);
1636+
kv[key] = values.begin();
1637+
if (values.size() > cap) {
1638+
kv.erase(values.back().key);
1639+
values.pop_back();
1640+
}
1641+
} else {
1642+
kv[key]->value = value;
1643+
move_front(key);
1644+
}
1645+
}
1646+
1647+
private:
1648+
int move_front(int key) {
1649+
LRUNode node = *kv[key];
1650+
values.erase(kv[key]);
1651+
values.push_front(node);
1652+
kv[key] = values.begin();
1653+
}
1654+
1655+
private:
1656+
int cap;
1657+
std::list<LRUNode> values;
1658+
std::unordered_map<int, list<LRUNode>::iterator> kv;
1659+
};
14751660
```

Interview/Company/intuitive/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ please.
6868
- A abstration data struture that groups all modules and their fans together as well as handles fan hardware related operation such as module temperature value updating and fan speed duty cycle seting and calculation.
6969

7070
***Fan Control Client:***
71-
- A program that performs temperature reading and send them out either as a reply to server's request, nor as whenever it feels appropriate to (e.g. need urgent fan speed increase due to overheat)
71+
- A program that performs temperature reading and send them out either as a reply to server's request, or as whenever it feels appropriate to (e.g. need urgent fan speed increase due to overheat)
7272
- In real-world scenario, client module process usually has other tasks. However in our app demo, client is assumed to have one task only - to read temperature and send out to server whenever it needs to.
7373

7474
***Server Msg Queue***

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
3. [Endianess Check](Data_Struct_Implementation/endianess/README.md)
5555
4. [Swap Endianess](Data_Struct_Implementation/endianessSwap/README.md)
5656
5. Signness Check
57-
6. [Array of Bits](Data_Struct_Implementation/bitsArray/README.md)
57+
6. [Array of Bits (bit sets/bit map)](Data_Struct_Implementation/bitsArray/README.md)
5858
7. Low Pass Filter
5959
8. [Memory map IO register manipulation](Data_Struct_Implementation/memoryMap/memory_map_io.md)
6060
9. Timer list
@@ -115,11 +115,11 @@
115115

116116
### D. Basic Algorithms
117117
1. [Sort](https://www.cnblogs.com/onepixel/p/7674659.html)
118-
1. [Bubble Sort](/Data_Struct_Implementation/bubbleSort/bubbleSort.md)
119-
2. [Merge Sort](/Data_Struct_Implementation/mergeSort/mergeSort.md)
120-
3. [Quick Sort](/Data_Struct_Implementation/quickSort/quicksort.md)
121-
4. [Heap Sort](/Data_Struct_Implementation/heapSort/heapSort.md)
122-
5. [Insertion Sort](/Data_Struct_Implementation/insertionSort/insertionSort.md)
118+
1. [Bubble Sort](Data_Struct_Implementation/bubbleSort/bubbleSort.md)
119+
2. [Merge Sort](Data_Struct_Implementation/mergeSort/mergeSort.md)
120+
3. [Quick Sort](Data_Struct_Implementation/quickSort/quicksort.md)
121+
4. [Heap Sort](Data_Struct_Implementation/heapSort/heapSort.md)
122+
5. [Insertion Sort](Data_Struct_Implementation/insertionSort/insertionSort.md)
123123
6. Binary Insertion Sort
124124
2. Search
125125
1. [Hash](Data_Struct_Implementation/hashTable/README.md)
@@ -173,6 +173,10 @@
173173
2. Memory Hardware
174174
3. [Virtual Addressing/Memory](Operating_System/virtual_memory.md)
175175
5. File System Management
176+
1. Operations on Files
177+
2. Virtual File System
178+
3. Concrete File System
179+
4. Inodes
176180
6. Device I/O Management
177181
7. RTOS
178182
1. FreeRTOS

Resources/caches.pdf

166 KB
Binary file not shown.

Resources/towards-ukernels.pdf

140 KB
Binary file not shown.

0 commit comments

Comments
 (0)