Software engineering notes

Algorithms

Bubble sort

How it works

Compare two values and move the larger value to the right in each iteration. This way, the greatest value will end up on the right side in each iteration and will be locked and excluded from subsequent iterations.

animated demonstration: https://commons.wikimedia.org/wiki/File:Bubble-sort-example-300px.gif

Complexity

Pros and Cons

Pros

Cons

Demonstration for unsorted numbers

input

7 5 6 1 3

i=4, j=0, j&j+1, (5<7, swap)

7 5 6 1 3
5 7 6 1 3
j j

i=4, j=1, j&j+1, (7>6, swap)

5 7 6 1 3
5 6 7 1 3
  j j

i=4, j=2, j&j+1, (7>1, swap)

5 6 7 1 3
5 6 1 7 3
    j j

i=4, j=3, j&j+1, (7>3, swap)

5 6 1 7 3
5 6 1 3 7
      j j

i=3, j=0, j&j+1, (5<6, do nothing)

5 6 1 3 7
j j

i=3, j=1, j&j+1, (6>1, swap)

5 6 1 3 7
5 1 6 3 7
  j j

i=3, j=2, j&j+1, (6>3, swap)

5 1 6 3 7
5 1 3 6 7
    j j

i=2, j=0, j&j+1, (5>1, swap)

5 1 3 6 7
1 5 3 6 7
j j

i=2, j=1, j&j+1, (5>3, swap)

1 5 3 6 7
1 3 5 6 7
  j j

i=1, j=0, j&j+1, (1<3, do nothing)

1 3 5 6 7
j j

end

Demonstration for sorted numbers

input

1 3 5 6 7

i=4, j=0, j&j+1, (1<3, swapped = false)

1 3 5 6 7
j j

i=4, j=1, j&j+1, (3<5, swapped = false)

1 3 5 6 7
  j j

i=4, j=2, j&j+1, (5<6, swapped = false)

1 3 5 6 7
    j j

i=4, j=3, j&j+1, (6<7, swapped = false)

1 3 5 6 7
      j j

swapped is false, then break the loop

end

for sorted numbers, it only takes O(n)

Insertion sort

How it works

Set a starting point (i) that moves forward in each iteration after comparing all the numbers prior to it and move the smaller number to the left.

animated demonstration: https://commons.wikimedia.org/wiki/File:Insertion-sort-example.gif

Complexity

Pros and Cons

Pros

Cons

Demonstration for unsorted numbers

input

7 8 5 2 4 6 3

i=1, j=1, j&j-1 (7<8, break loop)

7 8 5 2 4 6 3
i
j j

i is a starting point and j will be reduced by 1 in each i loop to compare 2 values

i=2, j=2, j&j-1 (8>5, swap)

7 8 5 2 4 6 3
7 5 8 2 4 6 3
  i
  j j

i=2, j=1, j&j-1 (7>5, swap)

7 5 8 2 4 6 3
5 7 8 2 4 6 3
  i
j j

i=3, j=3, j&j-1 (8>2, swap)

5 7 8 2 4 6 3
5 7 2 8 4 6 3
    i
    j j

i=3, j=2, j&j-1 (7>2, swap)

5 7 2 8 4 6 3
5 2 7 8 4 6 3
    i
  j j

i=3, j=1, j&j-1 (5>2, swap)

5 2 7 8 4 6 3
2 5 7 8 4 6 3
    i
j j

i=4, j=4, j&j-1 (8>4, swap)

2 5 7 8 4 6 3
2 5 7 4 8 6 3
      i
      j j

i=4, j=3, j&j-1 (7>4, swap)

2 5 7 4 8 6 3
2 5 4 7 8 6 3
      i
    j j

i=4, j=2, j&j-1 (5>4, swap)

2 5 4 7 8 6 3
2 4 5 7 8 6 3
      i
  j j

i=4, j=1, j&j-1 (2<4, break loop)

2 4 5 7 8 6 3
      i
j j

i=5, j=5, j&j-1 (8>6, swap)

2 4 5 7 8 6 3
2 4 5 7 6 8 3
        i
        j j

i=5, j=4, j&j-1 (7>6, swap)

2 4 5 7 6 8 3
2 4 5 6 7 8 3
        i
      j j

i=5, j=3, j&j-1 (5<6, break loop)

2 4 5 6 7 8 3
        i
    j j

i=6, j=6, j&j-1 (8>3, swap)

2 4 5 6 7 8 3
2 4 5 6 7 3 8
          i
          j j

i=6, j=5, j&j-1 (7>3, swap)

2 4 5 6 7 3 8
2 4 5 6 3 7 8
          i
        j j

i=6, j=4, j&j-1 (6>3, swap)

2 4 5 6 3 7 8
2 4 5 3 6 7 8
          i
      j j

i=6, j=3, j&j-1 (5>3, swap)

2 4 5 3 6 7 8
2 4 3 5 6 7 8
          i
    j j

i=6, j=2, j&j-1 (4>3, swap)

2 4 3 5 6 7 8
2 3 4 5 6 7 8
          i
  j j

i=6, j=1, j&j-1 (2<3, break loop)

2 3 4 5 6 7 8
          i
j j

end

Demonstration for sorted numbers

input

2 3 4 5 6 7 8

i=1, j=1, j&j-1 (2<3, break loop)

2 3 4 5 6 7 8
i
j j

i=2, j=2, j&j-1 (3<4, break loop)

2 3 4 5 6 7 8
  i
  j j

i=3, j=3, j&j-1 (4<5, break loop)

2 3 4 5 6 7 8
    i
    j j

i=4, j=4, j&j-1 (5<6, break loop)

2 3 4 5 6 7 8
      i
      j j

i=5, j=5, j&j-1 (6<7, break loop)

2 3 4 5 6 7 8
        i
        j j

i=6, j=6, j&j-1 (7<8, break loop)

2 3 4 5 6 7 8
          i
          j j

for sorted numbers, it only takes O(n)

Selection sort

How it works

Set a starting point (i) to find the minimum after it and swap it with the minimum. Lock the minimum and move the starting point forward in each iteration.

Complexity

Pros and Cons

Pros

Cons

Demonstration

input

2 8 1 3 9

i=0, minIdx=0, j=0, j+1 (2<8, do nothing)

2 8 1 3 9
i j
m

i=0, minIdx=0, j=1, j+1 (2>1, to be updated)

2 8 1 3 9
i   j
m

i=0, minIdx=2, j=1, j+1 (update minIdx)

2 8 1 3 9
i   j
    m

i=0, midIdx=2, j=2, j+1 (1<3, do nothing)

2 8 1 3 9
i   m j

i=0, midIdx=2, j=3, j+1 (1<9, do nothing)

2 8 1 3 9
i   m   j

i=0, midIdx=2, j=3, j+1 (end of iteration, swap min with i)

1 8 2 3 9
m   i   j

i=1, midIdx=1, j=1, j+1 (8>2, to be updated)

1 8 2 3 9
  m j
  i

i=1, midIdx=2, j=1, j+1 (update minIdx)

1 8 2 3 9
  i j
    m

i=1, midIdx=2, j=2, j+1 (2<3, do nothing)

1 8 2 3 9
  i m j

i=1, midIdx=2, j=3, j+1 (2<9, do nothing)

1 8 2 3 9
  i m   j

i=1, midIdx=2, j=3, j+1 (end of iteration, swap min with i)

1 2 8 3 9
  m i   j

i=2, midIdx=2, j=2, j+1 (8>3, to be updated)

1 2 8 3 9
    i j
    m

i=2, midIdx=3, j=2, j+1 (update midIdx)

1 2 8 3 9
    i j
      m

i=2, midIdx=3, j=3, j+1 (3<9, do nothing)

1 2 8 3 9
    i m j

i=2, midIdx=3, j=3, j+1 (end of iteration, swap min with i)

1 2 3 8 9
    m i j

i=3, midIdx=3, j=3, j+1 (8<9, do nothing)

1 2 3 8 9
      i j
      m

i=3, midIdx=3, j=3, j+1 (end of iteration, swap min with i)

1 2 3 8 9
      i j
      m

end

Merge sort

How it works

Divide the array into two equal-sized sub-arrays until each sub-array contains only one element. Then, merge each sub-array back into one, in order of value, until complete.

Complexity

Pros and Cons

Pros

Cons

Demonstration

                [6 5 3 1 8 7 2 4]
                  /           \
             [6 5 3 1]     [8 7 2 4]
              /    \         /    \
          [6 5]  [3 1]    [8 7]   [2 4]
          / \     / \      / \     / \
        [6] [5] [3] [1]  [8] [7] [2] [4]
           \ /    \ /     \ /     \ /
          [5 6]  [1 3]    [7 8]  [2 4]
              \  /          \   /
           [1 3 5 6]      [2 4 7 8]
                  \          /
               [1 2 3 4 5 6 7 8 ]

Quick sort

How it works

Choose a pivot element, partition the data set into two sub-arrays recursively based on the pivot element, and sort each sub-array by moving values less than the pivot to the left and values greater than the pivot to the right. When all sub-arrays contain only one element, the entire array is considered sorted.

Complexity

Pros and Cons

Pros

Cons

Demonstration

j
6 3 7 5 1 2 [4]    6 > 4
i

  j
6 3 7 5 1 2 [4]     3 < 4
i

  j
3 6 7 5 1 2 [4]     swap
i

  j
3 6 7 5 1 2 [4]     i+1
  i

    j
3 6 7 5 1 2 [4]     7 > 4
  i

      j
3 6 7 5 1 2 [4]     5 > 4
  i

        j
3 6 7 5 1 2 [4]     1 < 4
  i

        j
3 1 7 5 6 2 [4]     swap
  i

        j
3 1 7 5 6 2 [4]     i+1
    i

          j
3 1 7 5 6 2 [4]
    i

          j
3 1 7 5 6 2 [4]     2 < 4
    i

          j
3 1 2 5 6 7 [4]     swap
    i

          j
3 1 2 5 6 7 [4]     i+1
      i

             j
3 1 2 5 6 7 [4]     loop ends
      i

swap pivot and i

3 1 2 [4] 6 7 5

Now, all elements that are less than the pivot are before it, and all elements that are greater than the pivot are after it.

Then divide the array into two and repeat the sorting process recursively for each part using the same steps

[3 1 2]  4  [6 7 5]

and so on…

It’s in-place algorithm, why does it require extra space?

It doesn’t require an additional array to perform the sorting operation

However, QuickSort is a recursive algorithm, and each recursive call requires a small amount of memory that is allocated on the call stack, a system-provided data structure that tracks function calls.

Each recursive call to QuickSort is a new “layer” on the call stack, and the maximum depth of these layers is what gives rise to the space complexity of QuickSort.

The amount of memory used does not depend on the size of the input to the function (which is why we say it’s a constant amount), but on the number of recursive calls

So each recursive call to the QuickSort function requires a constant amount of space for its stack frame

Heap sort

How it works

  1. Build an max-heap/min-heap first from given unsorted array using the same idea of heapify
  2. In each iteration, swap the maximum with the last element, then run heapify to sort unsorted elements (exclude the elements that are shifted to the right)
    • pop the maximum, then heapify the rest of the elements
  3. Until the heap size reduces to 1, all elements are sorted

Complexity

Pros and Cons

Pros

Cons

Demonstration Part 1 - Make unsorted array a max-heap

For an array with 9 elements, only the first 4 elements need to be heapified in order to get max-heap. The reason why only the first 4 elements are needed is because the direction of heapify is downward, so the processes will take care of the whole heap

The example to explain above

          -> 8
           /   \
      -> 27     14 <-
        /  \   /  \
   -> 18    9 36   55
     / \
   41   21

  [ 8, 27, 14, 18, 9, 36, 55, 41, 21 ]
    *   *   *   *

If there are 10 elements in the array:

          -> 8
           /   \
      -> 27     14 <-
        /  \   /  \
   -> 18 -> 9 36   55
     / \   /
   41  21 8

  [ 8, 27, 14, 18, 9, 36, 55, 41, 21, 8 ]
    *   *   *   *  *

Now, start to work, the input for the following demonstration

   -> 7
     / \
 -> 1   2
   / \
  8   4

[7, 1, 2, 8, 4]
 *  *

the first 2 elements will need to be heapified

1st loop: swap value 8 with value 1 (heapify value 1)

      7
     / \
 -> 1   2
   / \
  8   4

      7
     / \
    8   2
   / \
  1   4

the heapify is downward, so it stops here

2nd loop: swap 7 with 8 (heapify value 7)

   -> 7
     / \
    8   2
   / \
  1   4

   -> 8
     / \
    7   2
   / \
  1   4

max-heap is done

Demonstration Part 2 - Sort the max-heap by shifting maximum to the right in each iteration

max-heap

      8
     / \
    7   2
   / \
  1   4

  [8, 7, 2, 1, 4]
1st loop

shift index 0 with the last one (swap value 8 with value 4)

      4
     / \
    7   2
   / \
  1   8

  [4, 7, 2, 1, 8]

do heapify for 0, 1, 2, 3 (without value 8), start with index 0

   -> 4
     / \
    7   2
   /
  1

  [4, 7, 2, 1, 8]

swap value 4 and value 7

      7
     / \
 -> 4   2
   /
  1

  [7, 4, 2, 1, 8]

do heapify, start from value 4

      7
     / \
 -> 4   2
   /
  1

  [7, 4, 2, 1, 8]

done (4 > 1, do nothing)

2nd loop

swap value 7 with value 1

      1
     / \
    4   2
   /
  7

  [1, 4, 2, 7, 8]

do heapify for index 0, 1, 2 (without value 7), start from value 1

   -> 1
     / \
    4   2

  [1, 4, 2, 7, 8]

swap value 1 and value 4

      4
     / \
 -> 1   2

  [4, 1, 2, 7, 8]
3rd loop

swap value 2 with value 4

      2
     / \
    1   4

  [2, 1, 4, 7, 8]

do heapify for index 0, 1 (without value 4), start from value 2

   -> 2
     /
    1

  [2, 1, 4, 7, 8]

done (2 > 1, do nothing)

4th loop

swap value 1 and value 2

      1
     /
    2

  [1, 2, 4, 7, 8]

done (no need to do heapify)

Counting sort

How it works

The key idea behind counting sort is to count the occurrences of each element in the input array, and use that information to determine the correct position of each element in the output array. This makes counting sort very efficient for data sets with a relatively small range of values, since it avoids the need for expensive comparisons between elements.

Complexity

n is the number of elements to be sorted and k is the range of values in the input array

Pros and Cons

Pros

Cons

Demonstration

input

 index     0     1     2     3
         -----------------------
 value  | 18  | 21  | 25  | 21  |
         -----------------------

min = 18, max = 25, size of count array = 8 (25-18+1)

Create a count array

 index     0     1     2     3     4     5     6     7
         -----------------------------------------------
 value  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |
         -----------------------------------------------
          (18)  (19)  (20)  (21)  (22)  (23)  (24)  (25)

Fill the count array with the number of occurrences

 index     0     1     2     3     4     5     6     7
         -----------------------------------------------
 value  |  1  |  0  |  0  |  2  |  0  |  0  |  0  |  1  |
         -----------------------------------------------
          (18)  (19)  (20)  (21)  (22)  (23)  (24)  (25)

Sum up values with predecessor values

 index     0     1     2     3     4     5     6     7
         -----------------------------------------------
 value  |  1  |  1  |  1  |  3  |  3  |  3  |  3  |  4  |
         -----------------------------------------------
          (18)  (19)  (20)  (21)  (22)  (23)  (24)  (25)

Create a sorted array with the same length as origin input

 index     0     1     2     3
         -----------------------
 value  |  0  |  0  |  0  |  0  |
         -----------------------

Now, start to fill the sorted array

To make sure this algorithm performs stable sorting, start with the last element 21 from the input

 index     0     1     2     3
         -----------------------
 value  | 18  | 21  | 25  | 21  |
         -----------------------


 index     0     1     2     3     4     5     6     7
         -----------------------------------------------
 value  |  1  |  1  |  1  |  3  |  3  |  3  |  3  |  4  |
         -----------------------------------------------
          (18)  (19)  (20)  (21)  (22)  (23)  (24)  (25)


 index     0     1     2     3
         -----------------------
 value  |  0  |  0  |  0  |  0  |
         -----------------------

Find the value of the count array 3 via value 21 from input array

 index     0     1     2     3
         -----------------------
 value  | 18  | 21  | 25  | 21  |
         -----------------------
                             |
                             |
 index     0     1     2     3     4     5     6     7
         -----------------------------------------------
 value  |  1  |  1  |  1  |  3  |  3  |  3  |  3  |  4  |
         -----------------------------------------------
          (18)  (19)  (20)  (21)  (22)  (23)  (24)  (25)


 index     0     1     2     3
         -----------------------
 value  |  0  |  0  |  0  |  0  |
         -----------------------

Fill the sorted array with 21 at the index 2, which comes from the value of count array 3 minus 1.

 index     0     1     2     3
         -----------------------
 value  | 18  | 21  | 25  | 21  |
         -----------------------
                             |
                             |
 index     0     1     2     3     4     5     6     7
         -----------------------------------------------
 value  |  1  |  1  |  1  |  3  |  3  |  3  |  3  |  4  |
         -----------------------------------------------
          (18)  (19)  (20)  (21)  (22)  (23)  (24)  (25)
                         ____/
                        /
 index     0     1     2     3
         -----------------------
 value  |  0  |  0  | 21  |  0  |
         -----------------------

Update the value of count array to 2 (3-1)

 index     0     1     2     3
         -----------------------
 value  | 18  | 21  | 25  | 21  |
         -----------------------
                             |
                             |
 index     0     1     2     3     4     5     6     7
         -----------------------------------------------
 value  |  1  |  1  |  1  |  2  |  3  |  3  |  3  |  4  |
         -----------------------------------------------
          (18)  (19)  (20)  (21)  (22)  (23)  (24)  (25)
                         ____/
                        /
 index     0     1     2     3
         -----------------------
 value  |  0  |  0  | 21  |  0  |
         -----------------------

Then next value is 25. Find the value of count array using the same step

 index     0     1     2     3
         -----------------------
 value  | 18  | 21  | 25  | 21  |
         -----------------------
                        \___________________________
                                                    \
 index     0     1     2     3     4     5     6     7
         -----------------------------------------------
 value  |  1  |  1  |  1  |  2  |  3  |  3  |  3  |  4  |
         -----------------------------------------------
          (18)  (19)  (20)  (21)  (22)  (23)  (24)  (25)


 index     0     1     2     3
         -----------------------
 value  |  0  |  0  | 21  |  0  |
         -----------------------

Find the index of sorted array 3 via the value of count array (4 minus 1)

 index     0     1     2     3
         -----------------------
 value  | 18  | 21  | 25  | 21  |
         -----------------------
                        \___________________________
                                                    \
 index     0     1     2     3     4     5     6     7
         -----------------------------------------------
 value  |  1  |  1  |  1  |  2  |  3  |  3  |  3  |  4  |
         -----------------------------------------------
          (18)  (19)  (20)  (21)  (22)  (23)  (24)  (25)
                               ______________________/
                              /
 index     0     1     2     3
         -----------------------
 value  |  0  |  0  | 21  |  0  |
         -----------------------

Update the sorted array and count array

 index     0     1     2     3
         -----------------------
 value  | 18  | 21  | 25  | 21  |
         -----------------------
                        \___________________________
                                                    \
 index     0     1     2     3     4     5     6     7
         -----------------------------------------------
 value  |  1  |  1  |  1  |  2  |  3  |  3  |  3  |  3  |
         -----------------------------------------------
          (18)  (19)  (20)  (21)  (22)  (23)  (24)  (25)
                               ______________________/
                              /
 index     0     1     2     3
         -----------------------
 value  |  0  |  0  | 21  | 25  |
         -----------------------

Next, the value 21

 index     0     1     2     3
         -----------------------
 value  | 18  | 21  | 25  | 21  |
         -----------------------
                  \_________
                            \
 index     0     1     2     3     4     5     6     7
         -----------------------------------------------
 value  |  1  |  1  |  1  |  2  |  3  |  3  |  3  |  3  |
         -----------------------------------------------
          (18)  (19)  (20)  (21)  (22)  (23)  (24)  (25)
                   __________/
                  /
 index     0     1     2     3
         -----------------------
 value  |  0  |  0  | 21  | 25  |
         -----------------------

Update the sorted array and count array

 index     0     1     2     3
         -----------------------
 value  | 18  | 21  | 25  | 21  |
         -----------------------
                  \_________
                            \
 index     0     1     2     3     4     5     6     7
         -----------------------------------------------
 value  |  1  |  1  |  1  |  1  |  3  |  3  |  3  |  3  |
         -----------------------------------------------
          (18)  (19)  (20)  (21)  (22)  (23)  (24)  (25)
                   __________/
                  /
 index     0     1     2     3
         -----------------------
 value  |  0  | 21  | 21  | 25  |
         -----------------------

Lastly, the last value 18

 index     0     1     2     3
         -----------------------
 value  | 18  | 21  | 25  | 21  |
         -----------------------
           |
           |
 index     0     1     2     3     4     5     6     7
         -----------------------------------------------
 value  |  1  |  1  |  1  |  1  |  3  |  3  |  3  |  3  |
         -----------------------------------------------
          (18)  (19)  (20)  (21)  (22)  (23)  (24)  (25)
           |
           |
 index     0     1     2     3
         -----------------------
 value  |  0  | 21  | 21  | 25  |
         -----------------------

Update the sorted array and count array

 index     0     1     2     3
         -----------------------
 value  | 18  | 21  | 25  | 21  |
         -----------------------
           |
           |
 index     0     1     2     3     4     5     6     7
         -----------------------------------------------
 value  |  0  |  1  |  1  |  1  |  3  |  3  |  3  |  3  |
         -----------------------------------------------
          (18)  (19)  (20)  (21)  (22)  (23)  (24)  (25)
           |
           |
 index     0     1     2     3
         -----------------------
 value  | 18  | 21  | 21  | 25  |
         -----------------------

Now, all the elements are sorted

 index     0     1     2     3
         -----------------------
 value  | 18  | 21  | 21  | 25  |
         -----------------------

end

Binary search

How it works

Find a specific value in a sorted array by repeatedly dividing the search interval in half. It works by comparing the target value to the middle element of the array. If the target value is less than the middle element, the search continues in the lower half of the array. If the target value is greater than the middle element, the search continues in the upper half of the array. The algorithm continues until the target value is found or the search interval is empty.

Complexity

Time: O(log n)

Pros and Cons

Pros

Cons

Demonstration

a sorted list, try to find 13

1 3 4 5 13 20 25 40 42 44 53

find the middle 20 and compare with it

1 3 4 5 13 20 25 40 42 44 53
           *

because 13 < 20, then look for the left half

1 3 4 5 13 20 25 40 42 44 53
|        |

find the middle 4 on the left half, then look for the right half as 13 > 4

1 3 4 5 13 20 25 40 42 44 53
|   *    |

find the middle 5 on the left half, then look for the right half as 13 > 5

1 3 4 5 13 20 25 40 42 44 53
    | *  |

find the node 13

1 3 4 5 13 20 25 40 42 44 53
        *

end

Traversal

tree

            9
          /   \
        4      20
       / \    /  \
      1   6  15   170

traversal

9 4 20 1 6 15 170

time complexity is O(n)

Pros

Cons

use cases

tree

            9
          /   \
        4      20
       / \    /  \
      1   6  15   170

traversal

9 4 1 6 20 15 170

The time complexity is O(n) regardless of the traversal method used

Pros

Cons

use cases

Why is BFS more effective in finding the shortest path compared to DFS?

inorder vs preorder vs postorder

Example 1

            9
          /   \
        4      20
       / \    /  \
      1   6  15   170

preorder

9 4 1 6 20 15 170

inorder

1 4 6 9 15 20 170

postorder

1 6 4 15 170 20 9

Example 2

              50
            /    \
          /        \
       30           80
      /  \        /    \
     1    47    70      100
         /  \     \     / \
       40    73    78  99  126
      /  \        /  \     /
     38  45      75  79  118

preorder

50 30 1 47 40 38 45 73 80 70 78 75 79 100 99 126 118

inorder

1 30 38 40 45 47 73 50 70 75 78 79 80 99 100 118 126

postorder

1 38 45 40 73 47 30 75 79 78 70 99 118 126 100 80 50

Why does BFS not have inorder, preorder, and postorder implementations, while DFS does?

What’s the pros and cons of recursive solution?

Pros

Cons

Other sorting algorithms

shortest path (of a weighted graph)

Leetcode techniques

Cumulative Sum

A cumulative sum is a sequence of partial sums of a given sequence. For an array of numbers, the cumulative sum at a certain index is the sum of all elements up to that index.

If nums is [a, b, c, d], the cumulative sum array would be [a, a+b, a+b+c, a+b+c+d].

For example, the original input:

[1, 2, 3, 4]

cumulative sum array:

[1, 3, 6, 10]

Once you have the cumulative sum array, you can quickly calculate the sum of any subarray from index i to j.

For example

Terms

Dynamic Programming

Functional Programming

Interview

Top 6 Coding Interview Concepts

The 10 Most Important Concepts For Coding Interviews

ref