Game Development with Godot & GDScript Module 3
Arrays, dictionaries and packed data interview questions
Interview questions on arrays, dictionaries and packed data: warm-ups, then what each harder question tests and how a good answer starts.
Warm-up questions
Short questions with short answers, each answered in full.
-
Fresher
What is the difference between duplicate() and duplicate(true) on a GDScript array?
-
Fresher
How do you read a value from a GDScript dictionary when the key might be missing?
-
Fresher to mid
Why would you use a PackedVector2Array instead of an Array of Vector2 values?
Answered in the lesson Packed arrays and building strings fast
Questions with model answers
What each question tests, the first step of a good answer and the follow-ups to expect.
-
Fresher to mid
An inventory screen sorts items by rarity with sort_custom(). Its unit test with 8 items passes on every run, yet players with bags of 30 items say that items of equal rarity jump around whenever they pick something up. Explain why the test misses it, fix the sort, and improve the test.
- What it tests
- Knowing that Godot's sort is not stable, that arrays of up to 16 items hide it, and writing a comparison and a test that pin one order.
- First step
- Sort 30 items whose equal-rarity items start in a known order and check whether they keep it. On Godot 4.7.2 ties kept their order up to 16 items and moved from 17 on, so an 8-item test cannot see the problem.
- Follow-ups
-
- Which tie-breaker would you choose, and why not the item's old position?
- How would you make the test fail before the fix?
- Does sort() on plain ints have the same problem?
From the lesson Arrays and typed arrays
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
A settings menu starts with var settings = defaults and loads the save with settings.merge(saved, true). After a player sets the volume to 35, Reset to defaults (settings = defaults.duplicate()) keeps 35 instead of the default 80. Explain the bug, fix it, and say how you would stop it coming back.
- What it tests
- Seeing that = shares a dictionary, that merge() changes the dictionary it is called on, and protecting defaults with merged(), deep copies or a const.
- First step
- Print is_same(settings, defaults) right after loading. It prints true: settings is only a second name for defaults, so merge(saved, true) wrote the player's 35 into the defaults themselves, and every later copy of defaults carries it.
- Follow-ups
-
- Would declaring the defaults with const have caught it?
- The settings also hold a nested dictionary of key bindings. What else must change?
- How would a test catch this before players do?
From the lesson Dictionaries and typed dictionaries
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
A game counts kills in a dictionary keyed by enemy id, an int, saves it with JSON.stringify() and loads it back with JSON.parse_string(). After loading, kills.get(enemy_id, 0) returns 0 for every enemy, though the file holds the counts. What do you check, and how do you fix the loader?
- What it tests
- Knowing that a lookup matches the key's type as well as its value, that JSON keys are strings and its numbers come back as floats, and converting keys once while loading.
- First step
- Print the loaded dictionary and the type of one key. On Godot 4.7.2 the counts {3: 5, 7: 1} came back as { "3": 5.0, "7": 1.0 }: String keys and float values, so the int key 3 finds nothing.
- Follow-ups
-
- Why did saving and loading change the keys at all?
- Would a Dictionary[int, int] have helped?
- What should the loader do with a key that is not a number?
From the lesson Dictionaries and typed dictionaries
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
A drawing tool lets players sketch a path. Every mouse-motion event runs line.points.append(event.position) on a Line2D, and no point ever appears; nothing prints an error. Explain why, fix it, and say what the fix should look like for sketches of 10,000 points.
- What it tests
- Knowing that a built-in packed property returns a copy, that the change must be assigned back, and avoiding a full copy of a long path on every event.
- First step
- Print line.points.size() before and after one append. It does not change: reading line.points returned a copy, the append changed that copy, and nothing assigned it back to the Line2D.
- Follow-ups
-
- Why does line.points[0] = p fail in the same way?
- With the fix, every event copies the whole path twice. When does that matter, and what would you use instead?
- Would the same bug happen with a PackedVector2Array in your own variable?
From the lesson Packed arrays and building strings fast
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A combat log screen takes over a second to open after a long fight of 20,000 lines; it builds its text with log += line every time it opens. A short test fight opens instantly. How do you confirm the cause, and what do you change?
- What it tests
- Measuring before changing, knowing that += on a String copies the whole text, and replacing it with a PackedStringArray and join(), or keeping the lines ready during the fight.
- First step
- Time the build alone with Time.get_ticks_usec() at 5,000, 10,000 and 20,000 lines. If twice the lines cost about four times as long, the cost is the copying of +=, which grows with the square of the lines.
- Follow-ups
-
- Why does the short test fight not show the problem?
- Would adding each line to the text with += as the fight goes be better?
- How would you keep the log from growing without limit?
From the lesson Packed arrays and building strings fast
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
All Game Development with Godot & GDScript interview questions · The Game Development with Godot & GDScript track