Discrete Mathematics
Logic, proofs, counting, relations, graph structure and modular arithmetic — the language used to argue a program is correct. Interviewers probe it when you must justify a bound, not just code one.
part ofComputer science fundamentalsoverview, primer and where to startread it →on this pageshowhide
explore
- Symbolic Logic13 questions
- Truth Tables & Equivalence5 questions
- Normal Forms & Simplification4 questions
- Quantifiers & Predicates4 questions
- Proof Technique20 questions
- Contradiction & Contraposition5 questions
- Strong & Structural Induction5 questions
- Loop Invariants & Termination6 questions
- Pigeonhole & Extremal Arguments4 questions
- Relations & Order24 questions
- Set Algebra & Subsets6 questions
- Functions & Cardinality6 questions
- Equivalence Classes & Closure6 questions
- Posets & Lattices6 questions
- Counting & Combinatorics25 questions
- Product & Sum Rules5 questions
- Binomial Identities5 questions
- Inclusion-Exclusion Principle5 questions
- Linear Recurrences5 questions
- Probabilistic Bounds5 questions
- Graph Theory25 questions
- Handshake Lemma & Density5 questions
- Tree Characterizations5 questions
- Connectivity & Euler Tours5 questions
- Chromatic Number & Cliques5 questions
- Matchings & Vertex Covers5 questions
- Modular Arithmetic15 questions
- Congruence & Residue Classes5 questions
- Inverses and Bezout5 questions
- Euler's Totient & CRT5 questions
- Computer Scienceskillanchors this topic
- AI & Data Scientistrole
- Backend Developerrole
- Blockchain Developerrole
- Data Analystrole
- Data Engineerrole
- Forward Deployed Engineerrole
- Full Stack Developerrole
- Game Developerrole
- Java Backend Developerrole
- Kotlin Backend Developerrole
- Machine Learning Engineerrole
- Server-Side Game Developerrole
- Software Architectrole
questions
122 · 6 sectionsA filter allows a request when authenticated AND (hasRole OR isServiceCall); what is the equivalent reject condition with the negation pushed inward?
basics
~20 sReject when NOT authenticated OR (NOT hasRole AND NOT isServiceCall). De Morgan's laws flip each connective as the negation moves inward: the outer AND becomes OR, the inner OR becomes AND, and every atom is negated.
A spec says every queued job is eventually acknowledged: what refutes that claim, and what do passing runs establish?
basics
~20 sOne queued job that is never acknowledged refutes the claim completely; a single counterexample is a full disproof. Passing runs only fail to find one. They establish the claim only if they exhaust a finite domain.
An alert rule says 'if the retry budget is exhausted, the request fails' - which restatement of it is guaranteed to hold?
basics
~10 sOnly the contrapositive: 'if the request succeeded, the retry budget was not exhausted'. The converse and the inverse are different claims that can be false while the original rule still holds.
How does 'every request has some handler' differ from 'some handler serves every request', and which implies the other?
basics
~20 sQuantifier order decides whether the handler may vary per request. 'Some handler serves every request' fixes one handler for all of them and is the stronger claim: it implies 'every request has some handler', while the reverse fails.
Why does the rule 'if the retry budget is exhausted, the request fails' count as true for a request that never retried?
basics
~20 sMaterial implication is false on exactly one row: antecedent true, consequent false. A request that never exhausted its budget cannot produce that row, so the rule holds vacuously - and a vacuous truth is evidence of nothing.
A service truncates each key's digest to 32 bits as a fingerprint; why are collisions eventually unavoidable?
basics
~20 sA 32-bit fingerprint has only 2^32 possible values, so any 2^32 + 1 distinct keys must contain two that share one. That is the pigeonhole principle; a better function cannot avoid it, only a wider field delays it.
To attack by contradiction the claim that an allocator never hands one block to two callers, what do you assume?
basics
~20 sAssume the claim fails: that some run exists in which one block is held by two callers at once. That single assumed run is a concrete object you can trace until it forces something impossible.
Why prove the contrapositive of 'if a block is on the free list, no live pointer refers to it'?
basics
~20 sThe contrapositive — if a live pointer refers to a block, that block is not on the free list — states the same claim, but starts from a fact you can trace: a pointer, and the call that produced it.
When a reviewer calls an induction proof circular, what does the inductive hypothesis actually let you assume?
basics
~20 sOnly smaller cases, never the case being proved. The step establishes an implication - if the claim holds at k, it holds at k+1 - and the base case supplies the first true instance, so nothing is assumed about the target.
Why does proving that every integer above 1 factors into primes require strong induction?
basics
~20 sBecause a composite splits as n = a times b, where a and b can be far smaller than n - 1. A previous-value hypothesis says nothing about them; the strong form assumes the claim for every value below n, which does cover both factors.
In a document tag filter, which set operation returns documents tagged either label, and which returns documents tagged both?
basics
~20 sUnion gives 'either' — every document carrying at least one of the labels. Intersection gives 'both' — only documents carrying every selected label. As a user ticks more labels a union can only stay the same or grow; an intersection can only stay the same or shrink.
A merge pipeline buckets rows by a grouping key: what must that key's equality and its hash satisfy for lookups to stay correct?
basics
~20 sEquality must be an equivalence relation — reflexive, symmetric, transitive — and must stay stable while the key is stored. Equal keys must produce equal hashes; unequal keys are allowed to collide, and a collision never means equality.
A merge pipeline groups customer rows by a 'same person' rule: which three properties must that rule satisfy for the groups to be well defined?
basics
~20 sReflexivity, symmetry and transitivity. A rule with all three is an equivalence relation, and only then does it cut the rows into disjoint groups where every row lands in exactly one group, whichever row you start from.
A migration remaps every old record identifier to a new one: what does injectivity of that mapping guarantee, and what breaks without it?
basics
~20 sInjective means no two distinct old identifiers are sent to the same new identifier. Lose it and two separate records land on one row in the target: one write overwrites the other, references converge, and no rollback can tell them apart.
When does a remapping from old record identifiers to new ones have an inverse that translates any new identifier back?
basics
~20 sInjectivity alone gives an inverse only on the identifiers the remap actually produced. To translate back any identifier the new store holds, the remap must also be surjective onto that set — together, a bijection onto it.
In the expansion of (1 + x)^n, why does the coefficient of x^k count the k-member reviewer sets drawn from n engineers?
basics
~20 sExpanding (1 + x)^n means one (1 + x) factor per engineer, and each factor contributes either 1 (stay off the review) or x (join it). A product becomes x^k exactly when k engineers contributed x, so its coefficient counts k-member sets.
On an n-engineer roster, why does summing the number of possible reviewer sets of every size 0 through n give exactly 2^n?
basics
~20 sBoth totals count one collection: every reviewer set the roster can produce. Grouping that collection by size and adding the groups gives the sum of binomial coefficients; building a set by deciding in or out per engineer gives 2^n. One collection, two tallies, so they are equal.
A nightly job must count records matching none of k taint filters - why must the correction terms alternate in sign?
basics
~20 sSubtracting each filter's matches over-corrects records caught by several filters, so pairwise overlaps are added back, triples removed again, and so on. The alternating signs make every record matching at least one filter cancel to exactly zero.
Using inclusion-exclusion, how do you count the IDs from 1 to 1000 divisible by none of 2, 3 and 5?
basics
~10 sSubtract from 1000 the multiples of 2, 3 and 5, add back the multiples of 6, 10 and 15, then subtract the multiples of 30. That is 1000 - 734 = 266 surviving IDs.
In a length-n maintenance schedule where no two maintenance slots may be adjacent, why does the count satisfy f(n)=f(n-1)+f(n-2)?
basics
~10 sSplit on the last slot. Idle leaves any valid (n-1)-schedule; maintenance forces the previous slot idle, leaving any valid (n-2)-schedule. The two cases are disjoint and exhaustive, so their counts add.
In a replication mesh where each link joins two machines, why does the sum of machines' link counts equal twice the link count?
basics
~20 sEvery link has two ends, and each end raises exactly one machine's link count by one. Totalling link counts therefore counts every link twice. This is the handshake lemma: the degree sum equals 2E, so it is always even.
What does the chromatic number of a conflict graph tell a scheduler that joins clashing jobs by an edge?
basics
~20 sThe chromatic number is the fewest colours that label every vertex so no edge has one colour at both ends. On a conflict graph it is the minimum number of time slots any valid schedule can use.
In a fibre backbone graph, what makes a site a cut vertex and a span a bridge?
basics
~20 sA cut vertex is a site whose removal, together with its spans, leaves the network in more pieces than before; a bridge is a span whose removal alone does that. Both name single points of failure.
In a fibre network, when can one closed route use every span exactly once?
basics
~20 sExactly when every span lies in one connected piece and every site has even degree. Each visit to a site uses one span in and one out, so an odd degree makes a closed route over every span impossible.
A wiring plan gives each of seven machines exactly three replication links - why can no such mesh exist?
basics
~20 sThe degrees would sum to 7 x 3 = 21, which is odd, but every graph's degree sum equals twice its edge count and so must be even. The plan is impossible for any wiring, not just awkward ones.
Given ac congruent to bc modulo n, when may you cancel c and conclude that a is congruent to b modulo n?
basics
~20 sOnly when c and n share no common factor, that is when their greatest common divisor is 1. Otherwise the honest conclusion is a congruent to b modulo n divided by that greatest common divisor, which is weaker.
Bucketing keys by remainder modulo n collapses integers into residue classes: what exactly does that collapse preserve?
basics
~10 sAddition, subtraction and multiplication survive: the class of a sum or product depends only on the input classes, never on which representatives you picked. Order, cancellation and the exponent position do not survive.
A multiplicative inverse of a modulo n does not always exist - for which a does it exist, and why?
basics
~20 sA value a has a multiplicative inverse modulo n exactly when gcd(a, n) = 1. If they share a factor g > 1, every product a*x reduced modulo n is a multiple of g, and 1 never is.
How does the extended Euclidean algorithm turn gcd(a, n) = 1 into an actual inverse of a modulo n?
basics
~20 sIt returns integers x and y with ax + ny = gcd(a, n), by carrying those two coefficients alongside the remainders. When the gcd is 1, reducing modulo n drops the n*y term, so x is the inverse of a.
Euler's theorem shrinks a huge exponent: when computing g^k mod n, what licenses replacing k with k mod phi(n)?
basics
~20 sEuler's theorem gives g^phi(n) congruent to 1 mod n whenever gcd(g, n) = 1, so the powers of g cycle with a period dividing phi(n) and only k mod phi(n) matters. Without that coprimality the reduction is simply invalid.