rusinikita / trainer

GoLang interview prep questions. Terminal app with Go challenges and learning links
GNU General Public License v3.0
249 stars 8 forks source link

Map quiz #15

Open rusinikita opened 4 months ago

rusinikita commented 4 months ago

Interviewers asks about maps internal structure:

  1. Components of maps (buckets, hashFunc)
  2. In which order for range iterates over map
  3. In which order map printed

Trick of 2 and 3 is iterator starts at random position and stringer sorts elements.

We need to create code sample and questions to check 2 and 3 knowledge.

rusinikita commented 3 months ago

Consider to add question about map resize on insert.

Is insert to overwhelmed map time consuming?

Behaviour: map is not resized all at once, so it is not time consumed operation than regular insert

rusinikita commented 3 months ago

Links:

https://go.dev/src/runtime/map.go

Challenge code could be

import (
    "fmt"
)

func main() {
    var m map[int]bool
    // m := make(map[int]bool)

    // no panic: false. Zero value if no key. Zero map
    fmt.Println(m[0])

    for i := 1; i < 5; i++ {
        // panic: assignment to entry in nil map
        m[i] = i%2 == 0
    }

    // if panic fixed: prints keys in sorted order
    fmt.Println(m)

    var toPrint []int
    for key := range m {
        toPrint = append(toPrint, key)
    }

    // if panic fixed: prints keys in random order
    fmt.Println(toPrint)
}