awelgorithms

Draw the map after each operation

Updated

Before coding, write the map after every put, delete, and increment on a 3–5 key example. If you cannot, you do not yet know whether you overwrite, append, or put-if-absent. Each line of code should match one drawing.

What to focus on

Frequency drawings are key→count after each item. Two-sum drawings are the seen map after each index, plus the query that just ran. Nested drawings show outer and inner.

Worked examples

puts

a = [], put 4→2, put 1→8, put 4→0
  1. {4:2} / {4:2,1:8} / {4:0,1:8}

Result: three drawings

freq

a = [4, 1, 9, 1]
  1. {4:1} / {4:1,1:1} / {…9:1} / {…1:2}

Result: four drawings

two-sum

a = [4, 1, 9], ops = "two-sum target 10"
  1. {} query 6; {4:0} query 9; {4:0,1:1} query 1 hit

Result: query then put

Try it

Write drawOps(a, ops). a is the starting map or array. ops names the put/freq/two-sum trace. Print the map after each operation in the three traces. put 4→2, 1→8, 4→0: {4:2} then {4:2, 1:8} then {4:0, 1:8}. a = [4, 1, 9, 1] → four drawings. a = [4, 1, 9], ops = "two-sum target 10" → query then put.

keys and values are integers. Follow the example operations in order.