Skip to content
Learn Netverks

Lesson

Step 12/36 33% through track

arrays-and-vectors

Arrays and vectors

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Arrays and vectors: data structure and algorithm concepts with complexity analysis and interview-ready C++ examples.

Teams apply Arrays and vectors in every serious DSA project—skipping it leaves blind spots in analysis and reviews.

You will apply Arrays and vectors in contexts like: Interview loops, performance tuning, and foundational CS courses.

Compile and run C++17 snippets in the playground (`int main`, `std::cout`); after each run, state time and space complexity before moving on.

When you can explain the previous lesson's ideas in your own words.

C++ arrays and std::vector store elements in contiguous memory—O(1) random access, cache-friendly like NumPy ndarrays but with explicit types.

Array vs vector

  • Fixed array int a[5] — size known at compile time
  • std::vector<T> — dynamic size, push_back, size()
  • Iterator range: for (int x : v)

Common operations

  • Index access — O(1)
  • Append at end — amortized O(1)
  • Insert in middle — O(n) shift

Vector demo

#include 
#include 

int main() {
    std::vector v = {10, 20, 30};
    v[1] = 25;
    v.push_back(40);
    std::cout << v[0] << " " << v.back() << " size=" << v.size() << "\n";
    return 0;
}

Important interview questions and answers

  1. Q: Why contiguous memory?
    A: CPU cache locality—sequential access is fast; foundation for SIMD and NumPy-style layouts.
  2. Q: vector vs list?
    A: vector for index-heavy workloads; linked list rarely wins on modern hardware due to cache misses.

Self-check

  1. When use std::vector over raw array?
  2. Complexity of v[i] access?

Tip: Prefer std::vector for dynamic arrays—cache-friendly like NumPy contiguous buffers.

Interview prep

vector vs array?

vector dynamic size; array fixed compile-time size.

push_back cost?

Amortized O(1); occasional O(n) resize.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • vector vs array?
  • reserve() when?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump