0
0
Unityframework~20 mins

Pathfinding basics in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pathfinding Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Unity C# code snippet?

Consider this code that attempts to find a path using a simple grid and prints the path length.

Unity
using UnityEngine;
using System.Collections.Generic;

public class PathfindingTest : MonoBehaviour {
    void Start() {
        List<Vector2Int> path = new List<Vector2Int> {
            new Vector2Int(0,0),
            new Vector2Int(1,0),
            new Vector2Int(1,1),
            new Vector2Int(2,1)
        };
        Debug.Log("Path length: " + path.Count);
    }
}
APath length: 3
BPath length: 4
CPath length: 5
DPath length: 0
Attempts:
2 left
💡 Hint

Count how many points are in the path list.

🧠 Conceptual
intermediate
1:30remaining
Which data structure is best for storing nodes to explore in a breadth-first search (BFS)?

In pathfinding, BFS explores nodes level by level. Which data structure fits this behavior best?

ADictionary
BStack (LIFO)
CHashSet
DQueue (FIFO)
Attempts:
2 left
💡 Hint

BFS explores nodes in the order they are discovered, first in, first out.

🔧 Debug
advanced
2:00remaining
What error does this Unity C# pathfinding code produce?

Examine this code snippet that tries to access a dictionary of nodes by key.

Unity
using System.Collections.Generic;
using UnityEngine;

public class PathfindingDebug : MonoBehaviour {
    void Start() {
        Dictionary<Vector2Int, int> nodeCosts = new Dictionary<Vector2Int, int>();
        nodeCosts[new Vector2Int(0,0)] = 1;
        int cost = nodeCosts[new Vector2Int(1,1)];
        Debug.Log(cost);
    }
}
AKeyNotFoundException
BNullReferenceException
CIndexOutOfRangeException
DNo error, outputs 0
Attempts:
2 left
💡 Hint

Check if the key exists before accessing the dictionary.

📝 Syntax
advanced
1:30remaining
Which option correctly declares a method to calculate Manhattan distance between two Vector2Int points?

Manhattan distance is the sum of absolute differences of x and y coordinates.

Aint ManhattanDistance(Vector2Int a, Vector2Int b) => Mathf.Abs(a.x - b.x) + Mathf.Abs(a.y - b.y);
Bint ManhattanDistance(Vector2Int a, Vector2Int b) { return abs(a.x - b.x) + abs(a.y - b.y); }
Cint ManhattanDistance(Vector2Int a, Vector2Int b) { return Mathf.Abs(a.x - b.x) + Mathf.Abs(a.y - b.y); }
Dint ManhattanDistance(Vector2Int a, Vector2Int b) { return Mathf.abs(a.x - b.x) + Mathf.Abs(a.y - b.y); }
Attempts:
2 left
💡 Hint

Check correct method syntax and correct function names in Unity.

🚀 Application
expert
3:00remaining
What is the number of nodes visited by this A* pathfinding pseudocode on a 3x3 grid with no obstacles?

Given a 3x3 grid from (0,0) to (2,2), start at (0,0), goal at (2,2), and A* using Manhattan distance heuristic, how many nodes are visited?

Assume neighbors are up, down, left, right, no diagonals.

Unity
Start = (0,0)
Goal = (2,2)
Grid size = 3x3
Heuristic = Manhattan distance
Neighbors = 4 directions
No obstacles
A7
B6
C9
D5
Attempts:
2 left
💡 Hint

Trace the A* search expanding nodes with lowest f = g + h.