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 complement?−128?64?32?16?8?4?2?1

Write −86 as an 8-bit two's complement pattern. Read the pattern as an unsigned binary number: what is its value U in decimal?

N = −868 bit
U =
2.Number systemsHard
20-bit two's complement, N = ??sign???????????????????

What is the most negative integer N that can be stored in 20-bit two's complement?

20 bit
N =
3.Propositional logic & proofsMedium
¬a ∨ c¬a ∧ c¬a ∨ ¬c¬a ∧ ¬c

Which formula is logically equivalent to ¬(a ∧ c)? Click it.

¬(a ∧ c)
  1. 1¬a ∨ c
  2. 2¬a ∧ c
  3. 3¬a ∨ ¬c
  4. 4¬a ∧ ¬c
4.Propositional logic & proofsHard
∀x A(x)∃x ¬A(x)∃x A(x)∀x ¬A(x)

Which statement is equivalent to ¬∃x A(x), with the negation moved all the way inside? Click it.

¬∃x A(x)
  1. 1∀x A(x)
  2. 2∃x ¬A(x)
  3. 3∃x A(x)
  4. 4∀x ¬A(x)
5.Digital logic: gates, K-maps & flip-flopsMedium
ABC000111100101324576

Simplify F = A′BC′ + A′B + A′BC + A′C to its minimal sum-of-products form. How many literals L does the simplified expression contain?

F = A′BC′ + A′B + A′BC + A′C
L =
6.Digital logic: gates, K-maps & flip-flopsHard
DQ0​Q0​DQ1​Q1​DQ2​Q2​DQ3​Q3​DQ4​Q4​DQ5​Q5​DQ6​Q6​DQ7​Q7​CLKJOHNSON

A Johnson (twisted-ring) counter is made of 8 D flip-flops, with the inverted last output fed back to the first input, and starts at all zeros. How many different states N does it go through?

n = 8
N =
7.Number theory & modular arithmeticMedium

Find Euler’s totient φ(245), the number of integers from 1 to 245 that are coprime to 245.

n = 245 = 5·72
φ(245) =
8.Number theory & modular arithmeticHard

19 is prime. Use Fermat’s little theorem to find 2530415 mod 19.

p = 19k = 30415
2530415 mod 19 =
9.Algorithms & complexityMedium

Breadth-first search on an unweighted graph finds:

  1. AShortest paths in graphs that contain negative edge weights
  2. BA topological order in all graphs
  3. CThe minimum spanning tree
  4. DShortest paths in number of edges from the start node
10.Algorithms & complexityHard

To show that a new problem X is NP-hard, what must you do?

  1. AReduce X to a known NP-hard problem in polynomial time
  2. BShow that X has no polynomial algorithm for small inputs
  3. CShow that X can be verified in polynomial time
  4. DReduce a known NP-hard problem to X in polynomial time
11.Data structuresMedium

In a binary min-heap stored in an array (index starting at 0), the children of the node at index i are at:

  1. Ai/2 and i/2 + 1
  2. B2i + 1 and 2i + 2
  3. Ci + 1 and i + 2
  4. D2i and 2i + 1
12.Data structuresHard

The time complexity of decrease-key in a binary heap (given the element's position) is:

  1. AO(n log n)
  2. BO(log n)
  3. CO(1)
  4. DO(n)
13.Graph theoryMedium

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

4 × deg 33 × deg 5
L =
14.Graph theoryHard
134187217121011ABCDEFG

Kruskal’s algorithm builds a minimum spanning tree. Click the edge it adds as edge number 6; skipped edges do not count.

k = 6
  1. 1edge AC
  2. 2edge BC
  3. 3edge BD
  4. 4edge CE
  5. 5edge DE
  6. 6edge EF
  7. 7edge GB
  8. 8edge GC
  9. 9edge GD
15.Databases & SQLMedium

Which query lists customers who have never placed an order?

  1. ASELECT c.name FROM customers c INNER JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL
  2. BSELECT c.name FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id = NULL
  3. CSELECT c.name FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL
  4. DSELECT c.name FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NOT NULL
16.Databases & SQLHard

Boyce–Codd normal form (BCNF) requires that for every non-trivial functional dependency X → Y:

  1. AX contains NULL
  2. BY is atomic
  3. CY is a primary key
  4. DX is a superkey
17.Computer networksMedium

Using even parity, which parity bit is added to the data bits 1011001?

  1. A4
  2. B2
  3. C0
  4. D1
18.Computer networksHard

A DNS cache poisoning attack aims to:

  1. AMake a resolver store a false IP address for a domain
  2. BSteal Wi-Fi passwords
  3. CDelete DNS servers
  4. DOverload a server with traffic so that it stops answering queries
19.IPv4 subnettingMedium
/24 → /2611111111111111111111111111000000network bitshost bits

The network 192.168.10.0/24 must be divided into at least 3 equal subnets. If you borrow as few bits as possible, how many usable hosts H does each subnet have?

192.168.10.0/243 subnets
H =
20.IPv4 subnettingHard
last octet = 4012806403201608140201networkhost

A host has the address 10.20.90.4/27. What is the fourth octet x of the last usable host address in its subnet?

10.20.90.4/27
x =

3Solutions

1.Number systems

Answer: U = 170

Positional number systems: each digit is worth its value times a power of the base. In binary the places are worth 1, 2, 4, 8, 16, …; in hexadecimal 1, 16, 256, …, with A–F standing for 10–15. In 8-bit two's complement the leftmost bit is worth −128 instead of +128, which is how negative numbers are stored.

Invert and add one

86 = 0101 0110
inverted: 1010 1001
+1 → 1010 1010

Read it as unsigned

1010 1010₂ = 256 − 86 = 170

Answer

U = 170
2.Number systems

Answer: N = −524288

Positional number systems: each digit is worth its value times a power of the base. In binary the places are worth 1, 2, 4, 8, 16, …; in hexadecimal 1, 16, 256, …, with A–F standing for 10–15. In 8-bit two's complement the leftmost bit is worth −128 instead of +128, which is how negative numbers are stored.

Most negative value

−219 = −524288

Answer

N = −524288
3.Propositional logic & proofs

Answer: ¬a ∨ ¬c

Logical equivalence: two formulas are equivalent (≡) when they have the same value in every row of the truth table. A tautology is 1 in every row, a contradiction is 0 in every row, and a contingency is neither. Useful laws: De Morgan, ¬(a ∧ b) ≡ ¬a ∨ ¬b and ¬(a ∨ b) ≡ ¬a ∧ ¬b; a → b ≡ ¬a ∨ b ≡ ¬b → ¬a (contrapositive); ¬(a → b) ≡ a ∧ ¬b; and the distributive laws, a ∨ (b ∧ c) ≡ (a ∨ b) ∧ (a ∨ c).

The law

De Morgan: ¬(a ∧ b) ≡ ¬a ∨ ¬b. The ∧ turns into ∨ and both parts are negated.
¬(a ∧ c) ≡ ¬a ∨ ¬c

Check with the truth table

a = 1, c = 1: 0 | 1 0 0 0
a = 1, c = 0: 1 | 0 0 1 0
a = 0, c = 1: 1 | 1 1 1 0
a = 0, c = 0: 1 | 1 0 1 1

Answer

¬a ∨ ¬c

The columns after the bar are the options in the order shown.

4.Propositional logic & proofs

Answer: ∀x ¬A(x)

Quantifiers and arguments: ¬∀x P(x) ≡ ∃x ¬P(x) and ¬∃x P(x) ≡ ∀x ¬P(x). Move a negation inwards, swap every quantifier it passes, and negate the inner formula with the propositional laws, such as ¬(a → b) ≡ a ∧ ¬b. An argument is valid when the conclusion is 1 in every row in which all premises are 1. A single row with true premises and a false conclusion is a counterexample and shows the argument is invalid.

Move the negation inwards

¬∃x A(x) ≡ ∀x ¬A(x)

Answer

∀x ¬A(x)

The other options either keep a quantifier unchanged, change the order of the quantifiers, or negate the inner formula wrongly.

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

Answer: L = 4

Boolean algebra: the rules that simplify expressions are X + X′ = 1 and X·X′ = 0, X + X = X, absorption X + X·Y = X, the combining rule X·Y + X·Y′ = X, X + X′Y = X + Y and the consensus theorem XY + X′Z + YZ = XY + X′Z. De Morgan's laws turn (X·Y)′ into X′ + Y′ and (X + Y)′ into X′·Y′. A Karnaugh map applies these rules graphically: every group of two neighbouring cells is one use of XY + XY′ = X.

Minterms of the expression

A′BC′ → m(2)
A′B → m(2, 3)
A′BC → m(3)
A′C → m(1, 3)
ABC00011110010011131204050706

Group and read off

F = A′C + A′B

Count the literals

L = 4

Answer

L = 4
6.Digital logic: gates, K-maps & flip-flops

Answer: N = 16

Flip-flops and counters: a flip-flop changes only at the active clock edge. D copies its input (Q⁺ = D); T toggles when T = 1; JK holds for 00, resets for 01, sets for 10 and toggles for 11. An n-bit binary counter has 2ⁿ states and counts modulo 2ⁿ; a mod-N counter goes 0, 1, …, N − 1 and back to 0, and needs the smallest n with 2ⁿ ≥ N flip-flops. Each stage of a ripple counter halves the frequency, so the last output of a mod-N counter runs at fclk/N. A Johnson counter of n flip-flops has 2n states, a ring counter n.

Sequence for 8 flip-flops

00000000 → 10000000 → 11000000 → 11100000 → 11110000 → 11111000 → 11111100 → 11111110 → 11111111 → 01111111 → 00111111 → 00011111 → 00001111 → 00000111 → 00000011 → 00000001

Number of states

N = 16

Answer

N = 16
7.Number theory & modular arithmetic

Answer: φ(245) = 168

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).

Prime factors

245 = 5·72

Totient

φ(245) = φ(49)·φ(5) = 42·4 = 168

Answer

φ(245) = 168
8.Number theory & modular arithmetic

Answer: 2530415 mod 19 = 4

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.

Reduce the exponent

30415 mod 18 = 13
2530415 ≡ 2513 (mod 19)

Small power

2513 mod 19 = 4

Answer

2530415 mod 19 = 4
9.Algorithms & complexity

Answer: D · Shortest paths in number of edges from the start node

Why this is the answer

BFS visits nodes in order of distance (edges) from the source, so it finds shortest unweighted paths in O(V + E).

Answer

D · Shortest paths in number of edges from the start node
10.Algorithms & complexity

Answer: D · Reduce a known NP-hard problem to X in polynomial time

Why this is the answer

If a known hard problem can be transformed into X efficiently, a fast algorithm for X would solve the known problem too.

Answer

D · Reduce a known NP-hard problem to X in polynomial time
11.Data structures

Answer: B · 2i + 1 and 2i + 2

Why this is the answer

For 0-based arrays, the left child is at 2i + 1, the right child at 2i + 2, and the parent at ⌊(i − 1)/2⌋.

Answer

B · 2i + 1 and 2i + 2
12.Data structures

Answer: B · O(log n)

Why this is the answer

After decreasing the key, the element sifts up at most the height of the heap. Fibonacci heaps achieve O(1) amortised decrease-key.

Answer

B · O(log n)
13.Graph theory

Answer: L = 15

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 + 27 = 2(L + 7) − 2
L = 15

Answer

L = 15

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

14.Graph theory

Answer: edge EF

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.

Kruskal’s algorithm

1. DE (2)
2. BC (4)
3. CE (7)
4. GC (10)
5. AC (13)
6. EF (17)

edge number 6

EF

Answer

edge EF
15.Databases & SQL

Answer: C · SELECT c.name FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL

Why this is the answer

The left join keeps all customers and fills order columns with NULL when there is no match; IS NULL then picks exactly those customers.

Answer

C · SELECT c.name FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL
16.Databases & SQL

Answer: D · X is a superkey

Why this is the answer

BCNF demands that every determinant is a candidate key (superkey); 3NF allows an exception when Y is part of a candidate key.

Answer

D · X is a superkey
17.Computer networks

Answer: C · 0

Why this is the answer

1011001 contains four 1-bits, already an even number, so the parity bit is 0. With odd parity it would be 1.

Answer

C · 0
18.Computer networks

Answer: A · Make a resolver store a false IP address for a domain

Why this is the answer

Poisoning inserts forged records into a resolver's cache. DNSSEC adds signatures so resolvers can verify records.

Answer

A · Make a resolver store a false IP address for a domain
19.IPv4 subnetting

Answer: H = 62

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).

Bits to borrow

22 = 4 ≥ 3 → b = 2, /26

Hosts per subnet

26 − 2 = 62

Answer

H = 62
20.IPv4 subnetting

Answer: x = 30

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

25 = 32

Subnet of this host

network: ⌊4/32⌋·32 = 0
broadcast: 0 + 32 − 1 = 31
usable hosts: 1 … 30

Answer

x = 30

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