Stage 2 · Lesson 1 of 6
Arrays, dynamic arrays and strings
An stores items side by side in memory, like numbered boxes in a row. That is why reading or writing any position () is O(1). The price is that inserting or deleting in the middle means shifting everything after it, which is O(n).
A dynamic array grows itself when it fills up by moving to a bigger block of memory, so adding to the end is O(1) amortised. Examples are the Python list, Java ArrayList, C++ vector and JavaScript array. behave like arrays of characters.
In Python, Java and JavaScript they cannot be changed once made, so building one with += in a loop copies the whole string every time. Collect the pieces in a list or builder and join them once at the end.
A question to check yourself, an AI practice session and 2 cheat sheet entries come with the full roadmap. Unlock
Resources for this stage
Start with· follow one of these
- CoursePaidGrokking Data Structures & Algorithms for Coding Interviews
Design Gurus · Beginner · Subscription
Each structure from first principles, with solutions in Python, Java, C++ and JavaScript
- CourseFreeMIT 6.006 Introduction to Algorithms
MIT OpenCourseWare · Intermediate
Free: lectures 2 and 4 cover dynamic arrays, linked lists, sets and hashing
Practise
Keep handy
Also worth your time
How to pick a structure from the operations a problem needs