nab-sack: A NBA stats trivia website

4 minute read

Published:

I spent a lot of the past year preparing for and taking technical interviews. This meant a lot of Data Structures, Algorithms, Graphs, Counting, and a whole lot of general Leetcoding.

I was initially a little dubious of the applicability of these questions at first. Like when do I need to compute the Longest Increasing Path In a Matrix or Measure how much water can be trapped in a 2d grid?

But, I did manage to draw some connections to my other hobby/passion - NBA basketball! So, I began creating nab-sack. Nab-sack is a website for NBA algorithmic trivia games.

The current games and applications include:

1) nabsack

nabsack is inspired by the famous computer science knapsack problem (hence the punny name).

The basic premise is that you are a robber entering a house with a sack that has a fixed capacity. Your goal is to maximize your value while staying under the capacity. You could take the $1000 TV that weights 25 lbs, or maybe it’s a better idea to take the $500 diamond earrings that only weigh 0.5 lbs.

Extrapolating this to the NBA, imagine you are a GM operating under the salary cap. You want to construct the team with the highest points (fantasy points in our case), while being under the fixed cap.

Player salaries and fantasy points are given. You can take as many players as you want.

This problem can be solved using backtracking and the literal code I have on my AWS lambda function is this:

best_path = ['']
max_profit = [0]
def dfs(i, path, p):
    if i == len(players):
        if p > max_profit[0]:
            max_profit[0] = p
            best_path[0] = path
        return
    
    # Option 1: Skip current player
    a = dfs(i + 1, path, p)
    # Option 2: Get current player
    if costs[i] + sum([costs[i] for i in range(N) if path[i] == '1']) <= max_amount:
        new_path = path[:i] + '1' + path[i+1:]
        dfs(i + 1, new_path, p + profits[i])
dfs(0, '0'*N, 0)

The game self-updates every day with a new challenge and provides the optimal solution upon submission.

2) NBA BFS

NBA breadth first search utilizes the common Breadth First Search (BFS) algorithm to find the shortest path between two nodes in an unweighted graph.

Your goal is to find the minimum number of hops or players to get from the source to the destination. Where a player is connected to another if they were ever teammates.

For example, let’s say the source is Stephen Curry and the destination is Kobe Bryant.

One path is Stephen Curry –> Monta Ellis –> Stephen Jackson –> Metta World Peace –> Kobe Bryant

But the shortest path is Stephen Curry –> Steve Blake –> Kobe Bryant

Can you get the shortest path?

Users can customize their own game and get the optimal path upon submission.

3) Statstionary

Like ‘Pictionary’, the goal of Statstionary is to identify an NBA player solely based on his stats.

I think this is best illustrated by example. One question a user can get is the following one”

KD

So we want a player who started on the Seattle Supersonics, then went to the Oklahoma City Thunder, then the Golden State Warriors, then the Brooklyn Nets, and is now on the Phoenix Suns. The player scores a lot of points per game has several awards including an MVP. Sound like anyone?

Kevin Durant!

There is a daily challenge and players can create their own game as well.

4) The NBA Network

For this project, I went out on a technical limb and tried to work with a new JavaScript library known as ThreeJS. The goal was to visualize the network of NBA players in a 3d manner.

Players are ‘connected’ if they played on one of the filtered teams during the span selected span inclusive.

Users can also hover/click on nodes to see the player’s name and zoom in/zoom out by pinching.

The result was a pretty sweet network animation:

NBA Network

You can also customize your filters to specify teams/eras. So this network consists of Warriors and Suns players between 2023 and 2024.

Chris Paul

Evidently, Chris Paul is located in the middle of the network since he was traded from one team to the other this last year.

Those are the current games in nab-sack! I hope you enjoy and I hope to expand it more in the future!