Data Structures

Arrays

Basics

  • Definition: An ordered collection of elements, each identified by an index.
  • Types:
    • One-dimensional arrays
    • Multi-dimensional arrays (matrices)
  • Indexing: Accessing elements using their index.
  • Slicing: Extracting subsets of an array.

Operations on Arrays

  • Creating arrays:
    • Using the list constructor
    • Using list comprehension
    • Using the numpy.array Function (for NumPy arrays)
  • Accessing elements:
    • Using index
  • Modifying elements:
    • Assigning new values
  • Adding/removing elements:
    • append, insert, pop, remove
  • Iterating over arrays:
    • Using for loops
  • Sorting arrays:
    • Built-in sort method
    • Using sorted function
  • Searching for elements:
    • Linear search
    • Binary search (for sorted arrays)

Advanced Topics

  • Array manipulation:
    • Reversing
    • Concatenating
    • Copying
  • Multi-dimensional arrays:
    • Creating and accessing
    • Matrix operations (addition, subtraction, multiplication)
  • NumPy library:
    • Introduction and basic operations
    • Advanced features (broadcasting, slicing, indexing)

Practice Questions

Easy

  1. Create an array of integers and print the first and last elements.
  2. Given an array, find the sum of all its elements.
  3. Reverse an array in-place.
  4. Find the maximum and minimum elements in an array.
  5. Check if an array contains a given element.

Medium

  1. Implement linear search.
  2. Implement bubble sort.
  3. Find the second largest element in an array.
  4. Rotate an array by k positions.
  5. Given two arrays, find their intersection.

Hard

  1. Implement binary search.
  2. Merge two sorted arrays.
  3. Find the median of two sorted arrays of equal size.
  4. Find the longest common subsequence between two strings.
  5. Implement matrix multiplication.




Algorithms