Suggest an editImprove this articleRefine the answer for “What is an algorithm?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)An **algorithm** is a finite and unambiguous sequence of steps (rules) that transforms input data into output data, solving a problem in a finite number of operations with a predictable result and a measurable time and memory complexity. **Key point:** an algorithm does not solve a single concrete example but an entire class of problems (generality/universality) - all inputs that satisfy the conditions.Shown above the full answer for quick recall.Answer (EN)Image## Short answer An algorithm is a finite and unambiguous sequence of steps (rules) that transforms input data into output data, solving a problem in a finite number of operations with a predictable result and a measurable time and memory complexity. ## Detailed answer ### Definition An algorithm is a formal description of a procedure for solving a class of problems. It specifies which actions must be performed, and in what order, on the input data to obtain a correct result. What matters is not the specific language or technology, but the logic of the steps, their correctness, and their efficiency. ### Key properties - **Input data:** it is specified what data the algorithm accepts (it can also be empty). - **Output (result):** it is clearly defined what the algorithm returns and in what format. - **Finiteness:** execution terminates in a finite number of steps. - **Determinism (unambiguity):** for the same input data, the result and the sequence of actions are predictable (or a probabilistic model is specified, if the algorithm is randomized). - **Discreteness:** the algorithm consists of separate, executable elementary steps. - **Generality (universality):** it solves not one concrete example, but an entire class of problems (all inputs that satisfy the conditions). - **Correctness (effectiveness):** for valid inputs it produces a correct result that conforms to the specification. - **Efficiency and complexity:** an estimate of time and memory costs. Big-O is often used: `O(1)`, `O(log n)`, `O(n)`, `O(n log n)`, `O(n²)`, and so on; memory is assessed separately. ### Examples of algorithms - **Linear search:** checks elements sequentially. Time - `O(n)`, memory - `O(1)`. - **Binary search:** searches a sorted array by halving the range. Time - `O(log n)`, memory - `O(1)`. Requires the data to be pre-sorted. - **Sorting:** quicksort - `O(n log n)` on average, `O(n²)` in the worst case; insertion sort - `O(n²)`, but good on nearly sorted data. - **Hashing:** lookup/insertion in a hash table - `O(1)` on average, `O(n)` in the worst case. ### Code example ``` // Linear search - O(n) time, O(1) memory function linearSearch(arr, x) { for (let i = 0; i < arr.length; i++) { if (arr[i] === x) return i; } return -1; } // Binary search - O(log n) time, O(1) memory (the array must be sorted) function binarySearch(sorted, x) { let l = 0, r = sorted.length - 1; while (l <= r) { const m = l + ((r - l) >> 1); if (sorted[m] === x) return m; if (sorted[m] < x) l = m + 1; else r = m - 1; } return -1; } // Quicksort - average O(n log n), worst case O(n^2) function quickSort(a) { if (a.length <= 1) return a; const pivot = a[a.length >> 1]; const left = [], mid = [], right = []; for (const v of a) { if (v < pivot) left.push(v); else if (v > pivot) right.push(v); else mid.push(v); } return [...quickSort(left), ...mid, ...quickSort(right)]; } // Usage example const data = [5, 1, 9, 2, 7, 3]; const sorted = quickSort(data); // [1,2,3,5,7,9] const idx1 = linearSearch(sorted, 7); // 4 const idx2 = binarySearch(sorted, 7); // 4 (faster on large n) ``` ### Algorithms in web development - **Search and filtering in interfaces:** debouncing/throttling input handlers, efficient `O(n)` filters or indexing. - **Rendering and diffing:** tree comparison algorithms (virtual DOM), minimizing repaints. - **Routing and caching:** route lookup, LRU caches for data and assets (service worker). - **Event and queue processing:** batching, time slicing, task scheduling in the event loop. - **Security and hashes:** hash functions, signature comparison, integrity checks. ### How to answer in an interview 1. Give a clear definition: what an algorithm is and its purpose. 2. List the key properties (input/output, finiteness, determinism, correctness, complexity). 3. **Mention complexity analysis:** time and memory, Big-O. 4. Give 1-2 examples (linear/binary search, sorting) and a short code snippet. 5. Connect it to web development practice: how you applied it in a project. > **Example of a short answer:** An algorithm is a finite sequence of unambiguous steps for solving a problem. Input and output, correctness, and efficiency matter. For example, binary search on a sorted array runs in `O(log n)` time and `O(1)` memory; I used it to speed up lookups on a pre-sorted list. ### Typical pitfalls - **Confusing "algorithm" with "implementation in a language":** the definition should be technology-neutral. - **Not mentioning input/output or finiteness:** these criteria are mandatory. - **Ignoring complexity:** always discuss time and memory in terms of Big-O. - **Mistakes in preconditions:** for example, binary search only works on sorted data.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.