Sitemap

Arrays and Strings

3 min readSep 1, 2026

--

Static Arrays:

  • Static arrays are contiguous blocks of memory with a fixed size.
  • Each element has an index, starting from 0.
  • Accessing or modifying elements by index is O(1) (constant time).
  • Checking if a value exists, inserting, or deleting (except at the end) takes O(n) time because you may need to scan or shift all elements.
  • Size can’t be changed after creation; insertions/deletions that require resizing or shifting are inefficient.

Dynamic Arrays (Python Lists):

  • Dynamic arrays can change size, unlike static arrays.
  • Usually implemented under the hood by using static arrays and resizing (copying to a larger static array when full).
  • Appending at the end is mostly O(1); when the underlying static array is full, resizing takes O(n), but it happens rarely enough that the average time (amortized) is still O(1).
  • Inserting or deleting anywhere except at the end requires shifting elements, which is O(n).
  • Most operations are efficient, but constantly resizing or inserting/deleting in the middle is less so.

Strings in Python:

  • They are similar to arrays, but are immutable (cannot be changed).
  • To modify them (like appending), you need to create a new string, which takes O(n).
  • Accessing an element by index is O(1).
  • Operations like insertion, deletion, or searching take O(n).

Time Complexity:

Press enter or click to view image in full size

Space Complexity:

Press enter or click to view image in full size
import numpy as np

# creating numpy array
arr=np.array([1,2,3,4])
print('Array is:\n',arr)

#multi dimensional array
list_1 = [1, 2, 3, 4]
list_2 = [5, 6, 7, 8]
list_3 = [9, 10, 11, 12]
sample_array = np.array([list_1, list_2,list_3])
print('Multidimensional array is:\n',sample_array)
multi_arr= np.array([[1,2,3],[4,5,6],[7,8,9]])
print('Multidimensional array is:\n',multi_arr)

#np.fromiter()- create numpy array object from iterable object
iterable= (a*a for a in range(8))
np_iterable= np.fromiter(iterable,dtype= float)
print('np.fromiter() is:\n',np_iterable)

#np.arange()- return space value within given interval == np.arange([start, ]stop, [step, ]dtype=None)
print('np.arange():\n',np.arange(1,20 ,2 ,dtype=int))

#np.linspace()- evenly spaced number over 2 limits == np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
print('np.linspace:\n',np.linspace(3.5,10,3))


#np.empty()- create array with type and shape without value == np.empty(shape, dtype=float, order=’C’)
print('np.empty() with order C:\n',np.empty([3,3],dtype= int, order='C'))
print('np.empty() with order F:\n',np.empty([3,3],dtype= int, order='F'))

#np.ones()- create new array filled with ones
print('np.ones() is:\n',np.ones([4,3],dtype=float))

#np.zeros()- create new array filled with zeros
print('np.zeros() is:\n',np.zeros([4,3],dtype=float))

#copy and view array
arr=np.array([1,2,3,4])
no_copy= arr
print('No Copy array:\n',no_copy)
view= arr.view()
print('View array:\n',view)
print('Copy array:\n',arr.copy())

#copy array from another array
org_array = np.array([1.54, 2.99, 3.42, 4.87, 6.94, 8.21, 7.65, 10.50, 77.5])
print('Copy array from another array:\n',np.copy(org_array))
Array traversal problems fall into 5 main categories:

Here is example for more details regarding Array and String.

Array Traversal Patterns

  1. Single-pass traversal (left → right)
  2. In-place modification traversal

Basic Single-Pass Traversal (Left → Right)

Linear scan, compute something as you go.

  • Easy

1. Two Sum

217. Contains Duplicate

136. Single Number

1480. Running Sum of 1d Array

724. Find Pivot Index

  • Medium

122. Best Time to Buy and Sell Stock II

905. Sort Array By Parity

189. Rotate Array

Get Mansi Mehta’s stories in your inbox

Join Medium for free to get updates from this writer.

498. Diagonal Traverse

  • Hard

135. Candy

41. First Missing Positive

In-Place Modification Traversal

  • Easy

283. Move Zeroes

  • Medium

80. Remove Duplicates from Sorted Array II

  • Hard

57. Insert Interval

String Traversal Patterns — LeetCode

String traversal problems fall into these patterns:

  1. Character-by-character traversal
  2. Reverse traversal
  3. Center-expansion traversal

--

--

Mansi Mehta
Mansi Mehta

Self motivated, Self learner, Enthusiastis for new tech, Reader and Poetry Writer