liveBook · Manning

Linear time complexity

As explained in Elixir in Action, Third Edition

Linear time complexity is asymptotic running time that grows proportionally with the size of the input.

Linear time complexity describes work that grows proportionally with input size. In Elixir, list operations commonly have O(n) complexity because lists are singly linked and must be traversed. section 2.4.4

Recognize linear behavior in common list operations:

  • length/1 traverses the entire list. section 2.4.4
  • Enum.at/2 walks from the beginning to the requested position. section 2.4.4
  • Replacing or inserting at an arbitrary position, appending, and concatenating lists generally require O(n) work. section 2.4.4

Choosing a collection

Use lists for dynamic, variable-sized collections and sequential processing. When direct access matters, prefer tuples, maps, or a higher-level data structure instead of repeatedly traversing a list. section 2.4.4

iex(3)> Enum.at(prime_numbers, 3)
7

Observe positional access with Enum.at/2; reaching the fourth element illustrates traversal from the list's beginning. section 2.4.4

How Elixir in Action, Third Edition covers it

How deep it goes
Explains O(n) through singly linked-list traversal and names costly operations, without broader complexity analysis.
Examples
Uses length/1, Enum.at/2, list replacement, insertion, appending, and ++ as concrete O(n) examples.
What you can do after
Evaluate list operations and choose another collection when direct access is required.
How it connects
Connects list complexity with Enum, tuples, maps, and higher-level data structures.

Also explained in

Compare how 8 books explain linear time complexity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elixir in Action, Third Edition. The section links point to the parts of the book it draws on.