What are the "input data" and "output data" of an algorithm?
Short answer
Input data of an algorithm are the values/structures that the algorithm accepts and performs operations on. Output data is the result of the algorithm's work, returned after processing the input. Formally, an algorithm implements a mapping f: X → Y, where X is the set of valid inputs (domain) and Y is the set of possible outputs (codomain), sometimes accounting for errors: f: X → Y ∪ E.
Detailed breakdown
Definitions
- Input data: values, parameters, structures, or streams that are supplied to the algorithm for processing.
- Output data: the result of the computation - a value, structure, stream, or signal (including an error) that the algorithm returns outward.
Key properties and requirements
- Domain and validity: which values are considered valid inputs (types, ranges, format). The algorithm must be able to validate the input and correctly handle invalid cases.
- Determinism: for deterministic algorithms, the same input ⇒ the same output. For non-deterministic ones, the output may depend on randomness/environment.
- Explicit formats: clear input/output contracts (types, units, encodings, locale, sorting, pagination, etc.).
- Errors as part of the output: errors are also a form of output (an exception, a status code, Result<T, E>), even if they signal the impossibility of obtaining the main result.
- Separating side effects: logging, writing to a database, sending emails are not "output data" but side effects. Output is what is returned as the result of the computation.
Types of inputs/outputs (in practice)
- Scalar: numbers, strings, booleans, dates/timestamps.
- Structured: objects/dictionaries, records, JSON.
- Collections: arrays, lists, sets, maps.
- Streams: byte streams, iterators, reactive streams (Observable).
- State signals: status codes, success/error flags, exceptions, Result<E, T>.
Examples from development
- Sorting an array: input is an array of numbers/strings, output is a sorted array (of the same length).
- Login: input is an email/login and password; output is a token/session or an authorization error.
- REST endpoint GET /users?limit=10&offset=20: input is query parameters; output is a JSON list of users and pagination metadata or a 4xx/5xx error.
- Hash function: input is a byte string; output is a fixed hash string.
Formalization
An algorithm can be viewed as a function f: X → Y, where X is the set of valid inputs and Y is the set of outputs. In practice, it is often useful to explicitly account for errors: f: X → Y ∪ E, or to use types like Result<Y, E>. If some inputs are not supported, then f is partial: it is not defined on all of X, and this must be reflected in the contract.
Boundaries and edge cases
- Empty inputs: an empty array, an empty string - what should be returned? Often a neutral element or an error, depending on the task.
- Invalid values: null/undefined/NaN, an incorrect format, incorrect encoding.
- Overflows/leaky types: large numbers, long strings, large files.
- Non-determinism: dependency on time, randomness, network - fix this in the contract (RNG seeds, timeouts, retries).
Code example (JavaScript)
// Algorithm: compute the average of an array of numbers with input validation.
// Input: numbers: unknown
// Output: a result object { ok: boolean, value?: number, error?: string, meta?: object }
function safeAverage(numbers) {
if (!Array.isArray(numbers)) {
return { ok: false, error: 'numbers must be an array' }; // error as output
}
const filtered = numbers.filter(n => typeof n === 'number' && Number.isFinite(n));
if (filtered.length === 0) {
return { ok: false, error: 'no valid numbers' };
}
const sum = filtered.reduce((a, b) => a + b, 0);
const avg = sum / filtered.length;
return { ok: true, value: avg, meta: { count: filtered.length } }; // main output
}
// Usage examples:
// Input: [1, 2, 3]
// Output:
// { ok: true, value: 2, meta: { count: 3 } }
// Input: ['a', Infinity]
// Output:
// { ok: false, error: 'no valid numbers' }
// Bonus: parsing a query string (another algorithm)
function parseQuery(qs) {
const params = new URLSearchParams(qs.startsWith('?') ? qs.slice(1) : qs);
const result = {};
for (const [k, v] of params) {
if (k in result) result[k] = Array.isArray(result[k]) ? result[k].concat(v) : [result[k], v];
else result[k] = v;
}
return result;
}
// Input: '?q=test&tags=js&tags=algo'
// Output: { q: 'test', tags: ['js', 'algo'] }How to answer briefly in an interview
- Give a definition: "Input is what the algorithm accepts, output is what it returns; an algorithm is a mapping f: X → Y".
- Add a note about errors: "Errors are also a kind of output (for example, an exception or Result<E, T>)".
- Give 1-2 practical examples from web development (login, sorting, a REST request).
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.