Computer Science Fundamentals: cram pack

Formula sheets, the key method steps for every topic and 20 practice problems, with full solutions at the back.

New problemsBack to the course

In the print window, choose “Save as PDF” to keep a copy on your device.

1Formulas and methods

Number systems

Place values
binary 2k, octal 8k, hex 16k
To decimal
Σ dk·bk
From decimal
divide by the base, read the remainders bottom-up
Hex ↔ binary
one hex digit = 4 bits
Two’s complement
invert the bits and add 1

Propositional logic & proofs

Rows of a truth table
2ⁿ
Implication
p → q ≡ ¬p ∨ q ≡ ¬q → ¬p
Negated implication
¬(p → q) ≡ p ∧ ¬q
Biconditional
p ↔ q ≡ (p → q) ∧ (q → p)
De Morgan
¬(p ∧ q) ≡ ¬p ∨ ¬q, ¬(p ∨ q) ≡ ¬p ∧ ¬q
Distributive laws
p ∨ (q ∧ r) ≡ (p ∨ q) ∧ (p ∨ r), p ∧ (q ∨ r) ≡ (p ∧ q) ∨ (p ∧ r)
Negated quantifiers
¬∀x P(x) ≡ ∃x ¬P(x), ¬∃x P(x) ≡ ∀x ¬P(x)
Modus ponens, modus tollens
p → q, p ⊢ q; p → q, ¬q ⊢ ¬p

Method

  1. Count the different variables: the truth table has 2ⁿ rows, starting with all of them 1.
  2. Give every subformula its own column and fill the columns from the inside out.
  3. Apply the rule of each connective: → is 0 only for 1 → 0, ↔ is 1 when both sides agree.
  4. For equivalences, compare the final columns row by row; for an argument, look only at the rows in which every premise is 1.
  5. To negate a quantified statement, move ¬ inwards, swap ∀ and ∃, and negate the inner formula.

Digital logic: gates, K-maps & flip-flops

Basic gates
A·B, A + B, A′, (A·B)′, (A + B)′, A ⊕ B
Boolean rules
X + X′ = 1, X·X′ = 0, X + XY = X, XY + XY′ = X
De Morgan
(X·Y)′ = X′ + Y′, (X + Y)′ = X′·Y′
Karnaugh map order
00, 01, 11, 10 (Gray code)
Group of 2ᵏ cells
loses k variables
4-to-1 multiplexer
F = A′B′I0 + A′BI1 + AB′I2 + ABI3
Flip-flops
D: Q⁺ = D, T: Q⁺ = T ⊕ Q, JK: Q⁺ = JQ′ + K′Q
Counters
n-bit: 2ⁿ states; mod-N: smallest n with 2ⁿ ≥ N; fout = fclk/N
Ripple counter
fmax = 1/(n·tpd)
Ring and Johnson counters
n and 2n states

Method

  1. For a circuit, work from the inputs to the output one gate at a time and write the value on every wire, or write its expression and fill in the truth table.
  2. For simplification, put the 1s (and the don’t-cares as X) into a Karnaugh map with Gray-code rows and columns: 00, 01, 11, 10.
  3. Cover the 1s with as few and as large groups of 1, 2, 4, 8 or 16 cells as possible, wrapping round the edges; each group is one product term made of the variables that stay constant. Group the 0s for the POS form.
  4. For flip-flops and counters, read the inputs at each active clock edge, write the state after every edge, and remember that an n-bit counter wraps round after 2ⁿ states.

Number theory & modular arithmetic

Division with remainder
a = q·n + r, 0 ≤ r < n
Euclid
gcd(a, b) = gcd(b, a mod b)
Least common multiple
lcm(a, b) = a·b/gcd(a, b)
Inverse
a·a⁻¹ ≡ 1 (mod n), gcd(a, n) = 1
Linear congruence
a·x ≡ b → x ≡ a⁻¹·b (mod n)
Euler’s totient
φ(n) = n·∏(1 − 1/p), φ(pᵏ) = pᵏ − pᵏ⁻¹
Fermat
aᵖ⁻¹ ≡ 1 (mod p)
RSA
n = pq, φ = (p − 1)(q − 1), d = e⁻¹ mod φ, c = mᵉ mod n, m = cᵈ mod n

Method

  1. Reduce every number modulo n first; remainders lie between 0 and n − 1.
  2. For a gcd, divide repeatedly with Euclid’s algorithm; the last nonzero remainder is the gcd.
  3. For an inverse, run the extended algorithm and keep the coefficient of a; add n if it is negative.
  4. Solve a·x ≡ b by multiplying with a⁻¹; combine two congruences by writing x = r₁ + m₁k.
  5. For powers, write the exponent in binary and square repeatedly; in RSA, d = e⁻¹ mod φ(n) with φ(n) = (p − 1)(q − 1).

Graph theory

Handshake lemma
Σ deg(v) = 2|E|
Complete graphs
|E(Kn)| = n(n − 1)/2, |E(Km,n)| = m·n
Regular graph
|E| = n·k/2
Tree and forest
|E| = n − 1, |E| = n − c
Leaves of a tree
L = 2 + Σ (deg(v) − 2) over deg(v) ≥ 3
Euler
circuit: all degrees even; path: exactly 2 odd degrees
Hamilton circuits in Kn
(n − 1)!/2
Dijkstra update
d(v) = min(d(v), d(u) + w(u, v))
Walks of length L
(Mᴸ)ij

Method

  1. Write down the degree of every vertex; the degrees add up to twice the number of edges.
  2. For Euler questions, check that the graph is connected and count the vertices with odd degree.
  3. For trees, use n − 1 edges and the handshake lemma.
  4. For Dijkstra, keep a table of tentative distances and settle the smallest unsettled vertex each round.
  5. For a minimum spanning tree, sort the edges by weight and skip every edge that closes a cycle (Kruskal), or grow one tree from a start vertex (Prim).

IPv4 subnetting

Hosts per subnet
2(32 − prefix) − 2 (/31: 2, /32: 1)
Block size
256 − (mask octet)
Network address
IP AND mask
Broadcast address
IP OR (NOT mask): all host bits set to 1
Subnets from n borrowed bits
2n

2Practice problems

1.Number systemsMedium
8-bit two's complement0−12806403201608040201

Set the bits so they store −84 in 8-bit two's complement. Read as an unsigned number, what is the pattern's value U?

N = −848 bit
U =
2.Number systemsHard
click the bits to set them012806403201608040201

Set the bits to the 8-bit result of 1001 1000 >> 3. What is the result's value R in decimal?

1001 1000>> 3
R =
3.Propositional logic & proofsMedium
pqr(¬p ∧ r) ∧ qr → p((¬p ∧ r) ∧ q) ∨ (r → p)111?110?101?100?011?010?001?000?

In how many of the 8 rows of its truth table is ((¬p ∧ r) ∧ q) ∨ (r → p) true? Call this number N.

((¬p ∧ r) ∧ q) ∨ (r → p)
N =
4.Propositional logic & proofsHard

How many of the 16 rows of the truth table make ((r ∧ ¬s) → (p → q)) ∧ (q ∨ (¬p → ¬s)) true? Call this number N.

((r ∧ ¬s) → (p → q)) ∧ (q ∨ (¬p → ¬s))
N =
5.Digital logic: gates, K-maps & flip-flopsMedium
ABC00011110011001131204050706

Use the Karnaugh map to find the minimal product-of-sums (POS) form of F(A, B, C) = Σm(0, 2, 3). How many sum terms (factors) N does it have?

F(A, B, C) = Σm(0, 2, 3)
N =
6.Digital logic: gates, K-maps & flip-flopsHard
ABCD00011110000111100011130214050716012013015X1418X9111010

Find the minimal SOP form of F(A, B, C, D) = Σm(1, 3, 4, 6, 8, 11) + d(9, 14), where d marks the don't-care conditions. How many literals L does it contain?

F(A, B, C, D) = Σm(1, 3, 4, 6, 8, 11) + d(9, 14)
L =
7.Number theory & modular arithmeticMedium

Find the inverse of 93 modulo 97: the number x with 0 < x < 97 and 93·x ≡ 1 (mod 97).

a = 93n = 97
93−1 mod 97 =
8.Number theory & modular arithmeticHard

Use repeated squaring to find 51136 mod 71.

a = 51k = 136n = 71
51136 mod 71 =
9.Algorithms & complexityMedium

Which sorting algorithm is stable?

  1. AHeap sort
  2. BQuicksort (typical in-place version)
  3. CSelection sort (typical version)
  4. DMerge sort
10.Algorithms & complexityHard

What are the time and space costs of computing the n-th Fibonacci number with a loop that keeps only the last two values?

  1. AO(n) time and O(n) space
  2. BO(2ⁿ) time and O(1) space
  3. CO(log n) time and O(n) space
  4. DO(n) time and O(1) space
11.Data structuresMedium

A queue is implemented with two stacks: push onto an "in" stack, and pop from an "out" stack, refilling it from "in" when empty. What is the amortised cost per operation?

  1. AO(1)
  2. BO(n²)
  3. CO(n)
  4. DO(log n)
12.Data structuresHard

A Bloom filter can answer "is this element in the set?" with:

  1. AAnswers only for numbers
  2. BExact answers always
  3. CPossible false negatives but no false positives
  4. DPossible false positives but no false negatives
13.Graph theoryMedium

A tree has 2 vertices of degree 3, 2 vertices of degree 4, 2 vertices of degree 5, 4 vertices of degree 2, and all its other vertices are leaves (degree 1). How many leaves L does it have?

2 × deg 32 × deg 42 × deg 54 × deg 2
L =
14.Graph theoryHard
13158314101315ABCDEF

Use Dijkstra’s algorithm to find the length d(F) of the shortest path from A to F in the weighted graph.

start: Aend: F
d(F) =
15.Databases & SQLMedium

An employees table has a column manager_id that refers to another employee. How do you list each employee next to their manager's name?

  1. ACombine the table with itself using UNION ALL and a WHERE filter
  2. BUse GROUP BY manager_id only
  3. CCreate a second copy of the table on disk
  4. DJoin the employees table with itself using two aliases
16.Databases & SQLHard

An app runs UPDATE item SET qty = 4, version = 8 WHERE id = 1 AND version = 7, and reports a conflict if no row was updated. What technique is this?

  1. ADatabase sharding
  2. BWrite-ahead logging
  3. CStrict two-phase locking with row locks
  4. DOptimistic concurrency control
17.Computer networksMedium

Why does Wi-Fi use collision avoidance (CSMA/CA) instead of collision detection?

  1. AA wireless station cannot reliably detect collisions while it is transmitting
  2. BWi-Fi only allows one device per network
  3. CCollision detection only works on fibre cables, not on shared wireless radio channels
  4. DRadio signals never collide
18.Computer networksHard

The count-to-infinity problem occurs in which kind of routing protocol, and what helps against it?

  1. ADistance-vector; split horizon and poison reverse
  2. BStatic routing; adding default routes
  3. CLink-state; flooding link updates
  4. DPath-vector; longer AS paths
19.IPv4 subnettingMedium
/22 → /2711111111111111111111111111100000network bitshost bits

The network 10.35.112.0/22 is divided into equal subnets with prefix /27. How many subnets N are there?

10.35.112.0/22/27
N =
20.IPv4 subnettingHard
last octet = 50012806413211608041201networkhost

A host has the address 10.20.140.50/29. What is the last number (fourth octet) x of its subnet's broadcast address?

10.20.140.50/29
x =

3Solutions

1.Number systems

Answer: U = 172

Setting bits: each bit is worth a power of two (128, 64, …, 1). One hex digit is exactly four bits, and in 8-bit two's complement the leftmost bit is worth −128. Bitwise operations work on each bit position separately.

Invert and add one

84 = 0101 0100
inverted: 1010 1011
+1 → 1010 1100

Read it as unsigned

1010 1100₂ = 256 − 84 = 172

Answer

U = 172
2.Number systems

Answer: R = 19

Setting bits: each bit is worth a power of two (128, 64, …, 1). One hex digit is exactly four bits, and in 8-bit two's complement the leftmost bit is worth −128. Bitwise operations work on each bit position separately.

Bit by bit

Shifting right by 3 moves every bit 3 places to the right; bits that fall off the right end are lost and zeros come in on the left.

1001 1000 >> 3
= 0001 0011

Value of the result

0001 0011₂ = 19

Answer

R = 19
3.Propositional logic & proofs

Answer: N = 7

Truth tables: a formula with n different variables has 2ⁿ rows, one for every combination of truth values (1 = true, 0 = false). Work from the inside out and give every subformula its own column. ¬a flips the value; a ∧ b is 1 only when both are 1; a ∨ b is 0 only when both are 0; a → b is 0 only when a = 1 and b = 0; a ↔ b is 1 when both sides are equal.

Truth table row by row

p = 1, q = 1, r = 1: ¬p = 0, ¬p ∧ r = 0, (¬p ∧ r) ∧ q = 0, r → p = 1, ((¬p ∧ r) ∧ q) ∨ (r → p) = 1
p = 1, q = 1, r = 0: ¬p = 0, ¬p ∧ r = 0, (¬p ∧ r) ∧ q = 0, r → p = 1, ((¬p ∧ r) ∧ q) ∨ (r → p) = 1
p = 1, q = 0, r = 1: ¬p = 0, ¬p ∧ r = 0, (¬p ∧ r) ∧ q = 0, r → p = 1, ((¬p ∧ r) ∧ q) ∨ (r → p) = 1
p = 1, q = 0, r = 0: ¬p = 0, ¬p ∧ r = 0, (¬p ∧ r) ∧ q = 0, r → p = 1, ((¬p ∧ r) ∧ q) ∨ (r → p) = 1
p = 0, q = 1, r = 1: ¬p = 1, ¬p ∧ r = 1, (¬p ∧ r) ∧ q = 1, r → p = 0, ((¬p ∧ r) ∧ q) ∨ (r → p) = 1
p = 0, q = 1, r = 0: ¬p = 1, ¬p ∧ r = 0, (¬p ∧ r) ∧ q = 0, r → p = 1, ((¬p ∧ r) ∧ q) ∨ (r → p) = 1
p = 0, q = 0, r = 1: ¬p = 1, ¬p ∧ r = 1, (¬p ∧ r) ∧ q = 0, r → p = 0, ((¬p ∧ r) ∧ q) ∨ (r → p) = 0
p = 0, q = 0, r = 0: ¬p = 1, ¬p ∧ r = 0, (¬p ∧ r) ∧ q = 0, r → p = 1, ((¬p ∧ r) ∧ q) ∨ (r → p) = 1

Count the rows with value 1

((¬p ∧ r) ∧ q) ∨ (r → p): 1, 1, 1, 1, 1, 1, 0, 1
N = 7

Answer

N = 7
4.Propositional logic & proofs

Answer: N = 13

Truth tables: a formula with n different variables has 2ⁿ rows, one for every combination of truth values (1 = true, 0 = false). Work from the inside out and give every subformula its own column. ¬a flips the value; a ∧ b is 1 only when both are 1; a ∨ b is 0 only when both are 0; a → b is 0 only when a = 1 and b = 0; a ↔ b is 1 when both sides are equal.

Columns

¬s; r ∧ ¬s; p → q; (r ∧ ¬s) → (p → q); ¬p; ¬p → ¬s; q ∨ (¬p → ¬s); ((r ∧ ¬s) → (p → q)) ∧ (q ∨ (¬p → ¬s))

Split on p

p = 1: 7
p = 0: 6
N = 7 + 6 = 13

Answer

N = 13
5.Digital logic: gates, K-maps & flip-flops

Answer: N = 2

Karnaugh maps: the cells are ordered in Gray code (00, 01, 11, 10), so neighbouring cells, including the ones across the edges, differ in exactly one variable. Group the 1s in rectangles of 1, 2, 4, 8 or 16 cells, as large as possible and as few as possible, until every 1 is covered. Each group is one product term: the variables that stay constant inside it. A group of 2ᵏ cells loses k variables. Don't-cares (X) may be used to make groups bigger but never have to be covered. Grouping the 0s instead gives the minimal POS form.

ABC00011110011001131204050706

Group the 0s

Every group is a rectangle of 2ᵏ cells; edge cells are neighbours of the cells on the opposite edge.

(B + C′)
A′

Minimal POS form

F = (B + C′)A′
N = 2

Answer

N = 2
6.Digital logic: gates, K-maps & flip-flops

Answer: L = 8

Karnaugh maps: the cells are ordered in Gray code (00, 01, 11, 10), so neighbouring cells, including the ones across the edges, differ in exactly one variable. Group the 1s in rectangles of 1, 2, 4, 8 or 16 cells, as large as possible and as few as possible, until every 1 is covered. Each group is one product term: the variables that stay constant inside it. A group of 2ᵏ cells loses k variables. Don't-cares (X) may be used to make groups bigger but never have to be covered. Grouping the 0s instead gives the minimal POS form.

Prime implicants

The largest groups that can be formed from 1s and Xs.

B′D, BCD′, A′BD′, AB′C′
ABCD00011110000111100011130214050716012013015X1418X9111010

Minimal cover

F = B′D + A′BD′ + AB′C′
L = 8

Answer

L = 8
7.Number theory & modular arithmetic

Answer: 93−1 mod 97 = 24

Inverses and congruences: a has an inverse modulo n exactly when gcd(a, n) = 1. The extended Euclidean algorithm writes 1 = s·n + t·a, and then a⁻¹ ≡ t (mod n). A congruence a·x ≡ b (mod n) with gcd(a, n) = 1 has the solution x ≡ a⁻¹·b. Two congruences x ≡ r₁ (mod m₁) and x ≡ r₂ (mod m₂) with coprime moduli have one solution modulo m₁·m₂ (Chinese remainder theorem). Euler’s φ(n) counts the numbers from 1 to n that are coprime to n: φ(n) = n·∏(1 − 1/p).

Extended Euclidean algorithm

Each remainder r is written as r ≡ t·93 (mod 97).

r = 97, t = 0
r = 93, q = 1, t = 1
r = 4, q = 23, t = −1
r = 1, q = 4, t = 24

Inverse

1 ≡ 24·93 (mod 97)
x = 24
check: 93·24 = 2232 = 23·97 + 1

Answer

93−1 mod 97 = 24
8.Number theory & modular arithmetic

Answer: 51136 mod 71 = 48

Modular powers and RSA: compute aᵏ mod n by repeated squaring: write k in binary, square, reduce after every step and multiply the powers whose bit is 1. Fermat: aᵖ⁻¹ ≡ 1 (mod p) for a prime p not dividing a, so the exponent can be reduced modulo p − 1. RSA: choose primes p and q, n = p·q and φ(n) = (p − 1)(q − 1); the public exponent e is coprime to φ(n), and the private exponent is d = e⁻¹ mod φ(n). Encrypt with c = mᵉ mod n and decrypt with m = cᵈ mod n.

Binary exponent

136 = 100010002 = 128 + 8

Repeated squaring

511 ≡ 51 (mod 71)
512 ≡ 45 (mod 71)
514 ≡ 37 (mod 71)
518 ≡ 20 (mod 71)
5116 ≡ 45 (mod 71)
5132 ≡ 37 (mod 71)
5164 ≡ 20 (mod 71)
51128 ≡ 45 (mod 71)

Multiply the needed powers

51136 ≡ 20·45 ≡ 48 (mod 71)

Answer

51136 mod 71 = 48
9.Algorithms & complexity

Answer: D · Merge sort

Why this is the answer

Merge sort is stable when merging takes from the left half first on ties. In-place quicksort, heap sort and selection sort are usually not stable.

Answer

D · Merge sort
10.Algorithms & complexity

Answer: D · O(n) time and O(1) space

Why this is the answer

Each step needs only the two previous values, so the table of the bottom-up method can be replaced by two variables.

Answer

D · O(n) time and O(1) space
11.Data structures

Answer: A · O(1)

Why this is the answer

Each element is pushed and popped at most twice in total, so although one refill can take O(n), the average over a sequence is O(1).

Answer

A · O(1)
12.Data structures

Answer: D · Possible false positives but no false negatives

Why this is the answer

If any bit for an item is 0, it is definitely absent; if all are 1 it is probably present. Bloom filters trade accuracy for very little memory.

Answer

D · Possible false positives but no false negatives
13.Graph theory

Answer: L = 14

Euler, Hamilton and trees: a connected graph has an Euler circuit (every edge once, back to the start) when every vertex has even degree, and an Euler path that is not a circuit when exactly two vertices have odd degree; the path then runs from one odd vertex to the other. A Hamilton circuit visits every vertex once; Kn has (n − 1)!/2 of them. A tree on n vertices has n − 1 edges, and a connected graph with m edges keeps a spanning tree after removing m − n + 1 edges.

Handshake lemma for a tree

L + 32 = 2(L + 10) − 2
L = 14

Answer

L = 14

Shortcut: L = 2 + Σ (d − 2) over the vertices with degree d ≥ 3.

14.Graph theory

Answer: d(F) = 29

Shortest paths and spanning trees: Dijkstra’s algorithm settles one vertex per round: the unsettled vertex with the smallest tentative distance. Its distance is then final, and every neighbour v gets d(v) = min(d(v), d(u) + w(u, v)). Kruskal’s algorithm builds a minimum spanning tree by taking the edges from light to heavy and skipping any edge that would close a cycle; Prim’s algorithm grows one tree from a start vertex by always adding the lightest edge that leaves it. A spanning tree of n vertices has n − 1 edges.

Dijkstra round by round

Each line: the vertex settled, its final distance, and the neighbours whose distance improves.

A (0): B: 13, C: 15
B (13): D: 16
C (15): E: 29
D (16): E: 26, F: 29
E (26)
F (29)

Shortest path

A → B → D → F
d(F) = 29

Answer

d(F) = 29
15.Databases & SQL

Answer: D · Join the employees table with itself using two aliases

Why this is the answer

A self-join such as FROM employees e JOIN employees m ON e.manager_id = m.id lets one alias be the employee and the other the manager.

Answer

D · Join the employees table with itself using two aliases
16.Databases & SQL

Answer: D · Optimistic concurrency control

Why this is the answer

Optimistic locking assumes conflicts are rare: it checks a version column when writing and fails if another transaction has changed the row meanwhile.

Answer

D · Optimistic concurrency control
17.Computer networks

Answer: A · A wireless station cannot reliably detect collisions while it is transmitting

Why this is the answer

A radio transmitter cannot hear other weak signals while sending, and some stations are hidden from each other. So 802.11 waits random backoff times and uses acknowledgements.

Answer

A · A wireless station cannot reliably detect collisions while it is transmitting
18.Computer networks

Answer: A · Distance-vector; split horizon and poison reverse

Why this is the answer

After a failure, distance-vector routers can keep advertising stale routes to each other, raising the metric slowly. Split horizon, poison reverse and a maximum metric limit this.

Answer

A · Distance-vector; split horizon and poison reverse
19.IPv4 subnetting

Answer: N = 32

IPv4 subnetting: an address has 32 bits. The prefix /p says how many leading bits identify the network; the remaining 32 − p bits number the hosts. A subnet has 2³²⁻ᵖ addresses, but the first (network address) and last (broadcast address) cannot be given to hosts, so 2³²⁻ᵖ − 2 hosts fit. Exceptions: a /31 (point-to-point link, RFC 3021) uses both of its 2 addresses for hosts, and a /32 is one single address (a single host or a loopback).

Borrowed bits

27 − 22 = 5

Number of subnets

N = 25 = 32

Answer

N = 32

Each /27 subnet has 25 − 2 = 30 usable hosts.

20.IPv4 subnetting

Answer: x = 55

IPv4 subnetting: an address has 32 bits. The prefix /p says how many leading bits identify the network; the remaining 32 − p bits number the hosts. A subnet has 2³²⁻ᵖ addresses, but the first (network address) and last (broadcast address) cannot be given to hosts, so 2³²⁻ᵖ − 2 hosts fit. Exceptions: a /31 (point-to-point link, RFC 3021) uses both of its 2 addresses for hosts, and a /32 is one single address (a single host or a loopback).

Block size

23 = 8

Subnet of this host

network: ⌊50/8⌋·8 = 48
broadcast: 48 + 8 − 1 = 55
usable hosts: 49 … 54

Answer

x = 55

ExamProblems · examproblems.com/courses/computer-science-fundamentals