Data Structures in Ruby

12 essential data structures for Ruby developers. Each one explained with Big O complexity, animated visuals, and real code samples you can copy.

Array

Array (frozen)

Fixed-size, contiguous block of memory. Elements are stored sequentially and accessed by index in constant time. The foundation of most other data structures.

7
3
9
1
5
[0]
[1]
[2]
[3]
[4]

arr[0] = 7

Complexity

Access by indexO(1)
SearchO(n)
Insert / DeleteO(n)

When to use

  • +You know the exact size at creation time
  • +You need the fastest possible index-based access
  • +Working with fixed-length data like matrices or buffers
Ruby
# Ruby arrays are dynamic, but can simulate fixed-size
nums = [10, 20, 30, 40, 50].freeze  # immutable reference

copy = nums.dup
copy[0] = 99                 # O(1) - direct index access
val = copy[2]                # O(1) - read by index

# Search
idx = copy.index(30)         # O(n) - linear search
has = copy.include?(40)      # O(n) - linear scan

# Sort
sorted = copy.sort           # O(n log n)
copy.sort!                   # O(n log n) - in-place

Dynamic Array

Array

Resizable array that automatically grows when capacity is exceeded. The most commonly used data structure in most languages. Doubles its internal storage when full, giving amortized O(1) appends.

7
3
9
1
5
[0]
[1]
[2]
[3]
[4]

arr[0] = 7

Complexity

Access by indexO(1)
SearchO(n)
AppendO(1)*
Insert / Remove (middle)O(n)

When to use

  • +You need a resizable collection (most common case)
  • +You frequently access elements by index
  • +You mostly add or remove at the end
Ruby
names = ["Alice", "Bob", "Charlie"]

names.push("Diana")             # O(1) amortized - append
names.insert(1, "Eve")          # O(n) - shifts elements
names.pop                       # O(1) - remove last
names.shift                     # O(n) - remove first

has = names.include?("Bob")     # O(n)
idx = names.index("Bob")        # O(n)

names.sort!                     # O(n log n) - in-place
squares = (0...10).map { |x| x * x }  # O(n) - functional transform

Stack

Array (push/pop)

Last-In-First-Out (LIFO) collection. Only the top element is accessible. Used for tracking state that must be unwound in reverse order.

arrow_downward top
10

Push 10

Complexity

PushO(1)
PopO(1)
PeekO(1)
SearchO(n)

When to use

  • +Undo/redo functionality
  • +Expression parsing and evaluation
  • +DFS traversal of trees and graphs
  • +Matching brackets, parentheses validation
Ruby
# Use Array as stack
stack = []

stack.push(10)            # O(1)
stack.push(20)
stack.push(30)

top = stack.last           # O(1) - peek -> 30
val = stack.pop            # O(1) - pop -> 30

# Interview pattern: valid parentheses
def valid?(s)
  pairs = { "(" => ")", "[" => "]", "{" => "}" }
  st = []
  s.each_char do |c|
    if pairs.key?(c) then st.push(pairs[c])
    elsif st.empty? || st.pop != c then return false
    end
  end
  st.empty?
end

Queue

Thread::Queue

First-In-First-Out (FIFO) collection. Elements are added at the back and removed from the front. Fundamental for breadth-first processing.

front
10
back

Enqueue 10

Complexity

EnqueueO(1)
DequeueO(1)
PeekO(1)
SearchO(n)

When to use

  • +BFS traversal of trees and graphs
  • +Task scheduling and job queues
  • +Message passing between components
  • +Rate limiting, buffering
Ruby
require "thread"

# Thread::Queue for thread-safe queue
q = Thread::Queue.new
q.push("Task A")           # O(1) - enqueue
q.push("Task B")
q.push("Task C")

val = q.pop                # O(1) - dequeue (blocks if empty)
q.empty?                   # check if empty

# Interview pattern: BFS using Array
def bfs(root)
  queue = [root]
  until queue.empty?
    node = queue.shift                   # O(n) - dequeue
    print "#{node.val} "
    queue.push(node.left) if node.left
    queue.push(node.right) if node.right
  end
end

Hash Map

Hash

Maps keys to values using a hash function for near-constant-time lookups. The single most important data structure for coding interviews. Every language has a built-in implementation.

0
age:30
1
2
name:Al
3
city:NY

hash("age") = 0

Complexity

InsertO(1)*
LookupO(1)
DeleteO(1)
Contains keyO(1)

When to use

  • +Two Sum and frequency counting patterns
  • +Caching computed results (memoization)
  • +Grouping data by a key
  • +Any problem requiring O(1) lookup by key
Ruby
prices = { "apple" => 3, "banana" => 5 }

prices["cherry"] = 2                 # O(1) - add
prices["apple"] = 10                 # O(1) - update
has = prices.key?("banana")          # O(1)
prices.delete("cherry")              # O(1)

count = prices.fetch("mango", 0)    # O(1) - safe lookup with default

# Interview pattern: Two Sum
def two_sum(nums, target)
  seen = {}
  nums.each_with_index do |num, i|
    need = target - num
    return [seen[need], i] if seen.key?(need)  # O(1)
    seen[num] = i                               # O(1)
  end
  []
end

Hash Set

Set

Unordered collection of unique elements. Uses hashing internally for O(1) membership testing. Supports mathematical set operations like union, intersection, and difference.

0
age:30
1
2
name:Al
3
city:NY

hash("age") = 0

Complexity

AddO(1)*
ContainsO(1)
RemoveO(1)
Union / IntersectO(n)

When to use

  • +Checking if an element exists in O(1)
  • +Removing duplicates from a collection
  • +Set operations: union, intersection, difference
  • +Visited tracking in graph traversal
Ruby
require "set"

s = Set[1, 2, 3, 4, 5]

s.add(6)                   # O(1)
s.delete(1)                # O(1)
has = s.include?(4)        # O(1) -> true

# Set operations
other = Set[4, 5, 6, 7]
s & other                  # intersection -> Set[4, 5, 6]
s | other                  # union
s - other                  # difference

# Interview pattern: contains duplicate
def contains_duplicate?(nums)
  nums.length != nums.to_set.length
end

Linked List

manual impl

Sequence of nodes where each node points to the next (singly linked) or both next and previous (doubly linked). Efficient insertion and deletion at any known position, but no index-based access.

5
12
8
20

traversing: 5

Complexity

Insert at head/tailO(1)
Remove (given node)O(1)
SearchO(n)
Access by indexO(n)

When to use

  • +Frequent insertion/deletion in the middle
  • +Implementing LRU cache (with a hash map)
  • +When you need a deque (double-ended queue)
  • +Problems involving pointer manipulation
Ruby
# Ruby has no built-in linked list - manual implementation
ListNode = Struct.new(:val, :next)

head = ListNode.new(1, ListNode.new(2, ListNode.new(3)))

# Traverse - O(n)
curr = head
while curr
  print "#{curr.val} "
  curr = curr.next
end

# Interview pattern: reverse a linked list
def reverse(head)
  prev = nil
  curr = head
  while curr
    nxt = curr.next         # save
    curr.next = prev        # reverse link
    prev = curr; curr = nxt
  end
  prev  # O(n) time, O(1) space
end

Sorted Set (BST)

sorted Array

Collection of unique elements maintained in sorted order, typically backed by a balanced binary search tree (red-black tree). Supports range queries and O(log n) min/max.

831215

search(8)

Complexity

AddO(log n)
ContainsO(log n)
RemoveO(log n)
Min / MaxO(log n)

When to use

  • +Maintaining a sorted collection of unique items
  • +Range queries (all elements between X and Y)
  • +Sliding window problems needing sorted order
  • +Leaderboards, ranking systems
Ruby
require "set"

# SortedSet was removed in Ruby 3.0 - use sorted array pattern
sorted = [5, 3, 8, 1, 9].sort    # [1, 3, 5, 8, 9]

# Insert maintaining order - O(log n) search + O(n) shift
idx = sorted.bsearch_index { |x| x >= 4 } || sorted.length
sorted.insert(idx, 4)            # [1, 3, 4, 5, 8, 9]

sorted.delete(3)                 # O(n) - remove

min_val = sorted.first           # O(1) -> 1
max_val = sorted.last            # O(1) -> 9

# Range query
between = sorted.select { |x| x >= 4 && x <= 8 }  # [4, 5, 8]

Sorted Map (BST)

sorted Hash

Key-value pairs maintained in sorted key order, typically backed by a balanced BST. Enables ordered iteration and range lookups that hash maps cannot provide.

831215

search(8)

Complexity

InsertO(log n)
LookupO(log n)
RemoveO(log n)
Iterate (sorted)O(n)

When to use

  • +You need sorted key-value pairs
  • +Ordered iteration over entries
  • +Range lookups by key
  • +When insertion order or sorted order matters
Ruby
# Ruby Hash preserves insertion order but not sorted order
sd = { "banana" => 2, "apple" => 5, "cherry" => 1 }

sd["date"] = 3                   # O(1) insert
val = sd["apple"]                # O(1) -> 5
sd.delete("cherry")              # O(1)

# Iterate in sorted key order - O(n log n)
sd.sort_by { |k, _| k }.each do |key, value|
  puts "#{key}: #{value}"
end
# apple: 5, banana: 2, date: 3

first_key = sd.keys.sort.first   # "apple"
last_key  = sd.keys.sort.last    # "date"

Priority Queue (Heap)

manual MinHeap

Collection where elements are dequeued by priority rather than insertion order. Typically implemented as a binary heap. Essential for shortest-path algorithms and top-K problems.

13579

min-heap

Complexity

InsertO(log n)
Extract min/maxO(log n)
PeekO(1)
SearchO(n)

When to use

  • +Dijkstra's shortest path algorithm
  • +Merge K sorted lists/streams
  • +Top-K / Kth largest element problems
  • +Event-driven simulation, scheduling
Ruby
# No built-in heap - manual min-heap implementation
class MinHeap
  def initialize = @items = []

  def push(val)                        # O(log n)
    @items << val; sift_up(@items.size - 1)
  end

  def pop                              # O(log n)
    return nil if @items.empty?
    @items[0], @items[-1] = @items[-1], @items[0]
    min = @items.pop; sift_down(0)
    min
  end

  def peek = @items.first              # O(1)

  private

  def sift_up(i)
    while i > 0 && @items[i] < @items[(i - 1) / 2]
      @items[i], @items[(i - 1) / 2] = @items[(i - 1) / 2], @items[i]
      i = (i - 1) / 2
    end
  end

  def sift_down(i) = nil # heapify down
end

Concurrent Hash Map

Concurrent::Map

Thread-safe hash map designed for concurrent read/write access from multiple threads. Uses fine-grained locking or lock-free techniques instead of a single global lock.

0
age:30
1
2
name:Al
3
city:NY

hash("age") = 0

Complexity

InsertO(1)*
LookupO(1)
DeleteO(1)
Atomic updateO(1)*

When to use

  • +Multi-threaded caching
  • +Shared state across threads or async tasks
  • +Producer-consumer patterns with keyed data
  • +When you need concurrent reads and writes
Ruby
# gem install concurrent-ruby
require "concurrent-ruby"

cache = Concurrent::Map.new

cache["counter"] = 0                    # O(1) - thread-safe write
val = cache["counter"]                  # O(1) - thread-safe read

# Atomic compute-if-absent
cache.compute_if_absent("sessions") { 0 }  # factory called once

# Atomic update
cache.compute("counter") { |v| v + 1 }     # thread-safe increment

# Merge - update or insert
cache.merge_pair("hits", 1) { |old| old + 1 }

# Safe iteration (snapshot semantics)
cache.each_pair { |k, v| puts "#{k}: #{v}" }

Memory View / Slice

String slice / IO::Buffer

Zero-copy view over a contiguous region of memory. Lets you reference a portion of an array or buffer without allocating new memory. Critical for performance-sensitive parsing and processing.

1
2
3
4
5
6

Span[0..3] = [1, 2, 3]

Complexity

Create sliceO(1)
Access by indexO(1)
SearchO(n)
CopyO(n)

When to use

  • +Parsing strings or binary data without copies
  • +Processing sub-arrays without allocation
  • +High-performance, zero-allocation code paths
  • +Interop with native or unmanaged memory
Ruby
# String slicing - returns a new string (copy)
data = "Hello, World!"
chunk = data[7, 5]              # "World" - O(k) copy
data[7, 5] = "Earth"           # mutate in place
puts data                       # "Hello, Earth!"

# Array slicing - returns a new array (copy)
nums = [10, 20, 30, 40, 50]
slice = nums[1, 3]             # [20, 30, 40] - O(k) copy

# String#byteslice for byte-level slicing
raw = "binary\x00data"
header = raw.byteslice(0, 6)   # O(k) - "binary"
body   = raw.byteslice(7..)    # O(k) - "data"

# IO::Buffer (Ruby 3.2+) for zero-copy memory views
buf = IO::Buffer.for(raw)      # wraps existing string memory

Big O Comparison

Average-case time complexity. * = amortized.

StructureAccessSearchInsertDelete
ArrayO(1)O(n)O(n)O(n)
Dynamic ArrayO(1)O(n)O(1)*O(n)
StackO(n)O(n)O(1)*O(1)
QueueO(n)O(n)O(1)*O(1)
Hash MapO(1)O(1)O(1)*O(1)
Hash SetN/AO(1)O(1)*O(1)
Linked ListO(n)O(n)O(1)O(1)
Sorted SetO(n)O(log n)O(log n)O(log n)
Sorted MapO(log n)O(log n)O(log n)O(log n)
Priority QueueO(n)O(n)O(log n)O(log n)
Concurrent MapO(1)O(1)O(1)*O(1)
Memory ViewO(1)O(n)N/AN/A

Which collection should I use?

I need to...Use
Store items by index, resize dynamicallyList / Dynamic Array
Map keys to values with O(1) lookupHashMap / Dictionary
Track unique items, check existence in O(1)HashSet / Set
Last-in-first-out (undo, DFS, brackets)Stack
First-in-first-out (BFS, task queues)Queue
Keep elements sorted at all timesSortedSet / TreeSet
Process items by priority (Dijkstra, top-K)PriorityQueue / Heap
Insert/delete at a known position in O(1)LinkedList
Sorted key-value pairsSortedDictionary / TreeMap
Thread-safe shared cacheConcurrentDictionary
Slice arrays/strings without copyingSpan / Slice / memoryview

Frequently Asked Questions

What are the most important data structures in Ruby?add

The most commonly used are dynamic arrays (List/ArrayList/vector), hash maps (Dictionary/HashMap/dict), and hash sets. For interviews, also know stacks, queues, trees, and priority queues. These cover 90%+ of coding interview problems.

Which Ruby data structure should I learn first?add

Start with the dynamic array and hash map. Together they solve the majority of interview problems. Then learn stacks (for DFS, bracket matching) and queues (for BFS). After that, tackle trees, heaps, and graphs.

Does Big O complexity change between languages?add

No. Big O measures algorithmic complexity, not language-specific performance. A hash map lookup is O(1) whether you use Python dict, Java HashMap, or C# Dictionary. Constant factors differ (C++ is faster than Python in wall-clock time), but Big O is the same.

Is there a built-in priority queue in Ruby?add

It depends on the language runtime. Check the Ruby standard library documentation for heap or priority queue support.

Know the structures.
Now use them under pressure.

Practice with an AI interviewer that asks you to implement, optimize, and explain data structure choices in real time.

Try a free mock interviewarrow_forward