Skip to main content

🏫 School Level Basics

One-line summary: Foundational mathematical and algorithmic building blocks β€” primes, GCD, palindromes, factorials, digit manipulation. Every DSA practitioner must write these from memory.


Diagram​

School Basics Overview School Basics GIF

Concept​

These problems build your intuition for:

  • Digit manipulation: extracting digits via % 10 and Math.floor(n/10)
  • Primality testing: trial division up to √n
  • GCD / LCM: Euclidean algorithm β€” gcd(a,b) = gcd(b, a%b)
  • Palindrome checking: two-pointer or reverse-half technique
  • Bit tricks: XOR swap, power-of-two check

Key Algorithms​

Euclidean GCD​

function gcd(a, b) { return b === 0 ? a : gcd(b, a % b); }
function lcm(a, b) { return (a / gcd(a, b)) * b; }

Sieve of Eratosthenes​

function sieve(n) {
const isPrime = new Array(n + 1).fill(true);
isPrime[0] = isPrime[1] = false;
for (let i = 2; i * i <= n; i++)
if (isPrime[i]) for (let j = i * i; j <= n; j += i) isPrime[j] = false;
return isPrime;
}

Digit Extraction​

function sumDigits(n) {
let sum = 0;
while (n > 0) { sum += n % 10; n = Math.floor(n / 10); }
return sum;
}

XOR Swap (no temp variable)​

let a = 5, b = 3;
a ^= b; b ^= a; a ^= b; // a=3, b=5

Complexity Summary​

ProblemTimeSpace
Sum 1..N (formula)O(1)O(1)
Prime check (trial division)O(√n)O(1)
Sieve of EratosthenesO(n log log n)O(n)
Factorial (iterative)O(n)O(1)
GCD (Euclidean)O(log min(a,b))O(1)
Palindrome checkO(n)O(1)

Arrays β€” Key Concepts​

Arrays are the most fundamental data structure: a contiguous block of memory with O(1) index access.

OperationTimeNotes
Access by indexO(1)Random access
Search (unsorted)O(n)Linear scan
Search (sorted)O(log n)Binary search
Insert at endO(1) amortizedDynamic array
Insert at indexO(n)Shift required
Delete at indexO(n)Shift required

Common pitfalls:

  • Off-by-one errors (< vs <=)
  • Mutating array while iterating
  • Forgetting to handle empty array edge cases

Practice Problems​

ProblemDifficultySolution
LC 9 β€” Palindrome NumberEasyView Solution
LC 125 β€” Valid PalindromeEasyView Solution
LC 1071 β€” GCD of StringsEasyView Solution
CC β€” ATM (HS08TEST)Easy
CC β€” Factorial (FCTRL)EasyView Solution
CC β€” Chef and Squares (CHFSQRS)Easy
CC β€” Number of Rectangles (NRECT)Easy
CC β€” Palindrome (PALIN)Easy
LC 66 β€” Plus OneEasy
LC 7 β€” Reverse IntegerMediumView Solution
LC 202 β€” Happy NumberEasy
LC 1 β€” Two SumEasy
LC 26 β€” Remove Duplicates from Sorted ArrayEasy
LC 27 β€” Remove ElementEasy
LC 35 β€” Search Insert PositionEasy
LC 58 β€” Length of Last WordEasy
LC 88 β€” Merge Sorted ArrayEasy
LC 118 β€” Pascal's TriangleEasy
LC 119 β€” Pascal's Triangle IIEasy
LC 136 β€” Single NumberEasy
LC 169 β€” Majority ElementEasy
LC 217 β€” Contains DuplicateEasy
LC 242 β€” Valid AnagramEasy
LC 283 β€” Move ZeroesEasy
LC 344 β€” Reverse StringEasy
LC 387 β€” First Unique Character in StringEasy
LC 412 β€” Fizz BuzzEasy
CC β€” Life, the Universe, and Everything (TEST)Easy
CC β€” Enormous Input Test (INTEST)Easy
CC β€” Reverse The Number (FLOW007)Easy
CC β€” Find Remainder (FLOW002)Easy

← Back to Home Β· Β© sparshjaswal