Recent Articles

Is a Number a Prime Number using Trial Division - Python Programming Challenge

In this Python tutorial, which is based on a Java II homework assignment, we use trial division to determine if a given number is a prime number or a composite number. If the number is not a prime number, we list the factors of the given number. It's a fun question that could easily be found as a programming challenge or as part of a technical job interview.

Read More

Reverse a Linked List is a Popular Technical Interview Question

Reversing a linked list is a popular technical interview question and one I see a lot on popular programming challenge websites. In this linked list tutorial we will create a custom LinkedList Class with a reverse method that will reverse a linked list. We will also create unit tests to test the algorithm using Python's Unit Testing Framework.

Read More

Detect Cycle in Linked List Using Floyd's Cycle-Finding Algorithm

One popular algorithm for detecting cycles in linked lists is Floyd's Cycle-Finding Algorithm, which is often called the tortoise and the hare algorithm. The algorithm uses 2 pointers, a fast pointer and a slow pointer. The fast pointer ( hare ) traverses the linked list 2 nodes at a time while the slow pointer ( tortoise ) traverses the linked list 1 node at a time. If these pointers ever point to the same node in the linked, there is a cycle in the linked list.

Read More

Coding a Queue Abstract Data Type using a Linked List in Python

In this tutorial I will create a Queue Abstract Data Type using a linked list in Python. The queue will implement the two most common operations of enqueue and dequeue to add and remove an item from the queue.

Read More

Coding a Stack Abstract Data Structure Using Linked List in Python

This linked list tutorial will implement a Stack abstract data structure and use a linked list as the underlying data structure for maintain state. The developer will use common stack operations, like push, pop, and peek, and be unaware of the underlying use of a linked list and constant time, O(1), algorithms to perform the operations.

Read More

Inserting New Node in a Linked List Using Python

Let's continue to focus on fundamental data structures in computer science and the linked list. In this tutorial, let's insert new values in a linked list such that the list maintains its list of values in ascending order. Therefore, it's not good enough to add a new value to the linked list at the beginning in O(1). We have to search through the list in O(n) to find the proper location to insert the new node in O(1). It's the popular technical interview question and programming challenge with a twist!

Read More

Linear Search of Linked List in Python

Linked lists are a fundamental data structure in computer science and a common subject of technical job interview questions and programming challenges. One of the simplest challenges is to search a linked list to see if it contains a given value.

Read More

Shell Sort

Shell sort, also called shellsort, is similar to insertion sort. Insertion sort uses a fixed distance of 1 when comparing items in an unsorted list. Shell sort compares items over a greater distance so that big differences in disorder can be fixed before comparing at smaller distances. The goal is to increase performance by minimizing the number of shifts that would have been performed by using just insertion sort.

Read More

Cocktail Sort

Cocktail sort is a sorting algorithm, like comb sort, that attempts to improve the performance of bubble sort by eliminating turtles. Turtles are small numbers at the end of the unsorted list that slowly move to the front of the list one position at a time using bubble sort.

Read More

Comb Sort Improves Bubble Sort by Eliminating Turtles

Comb sort is a sorting algorithm that improves upon bubble sort by comparing and swapping values farther apart to eliminate turtles, which are small values at the end of the unsorted list. By moving these turtles to the front of the list quickly, comb sort reduces the number of swaps that would normally occur in bubble sort. In this tutorial, I will discuss comb sort as compared to bubble sort and write an implementation of comb sort in Python.

Read More

Depth-First Search in Python - Recursive and Non-Recursive Programming

Earlier I showed how to do depth-first search in C# and breadth-first search in C#. In this article I want to show depth-first search in Python, using both a recursive and non-recursive solution, because I want to show an example of topological sorting of a directed acyclic graph and the algorithm is based on depth-first search. I am also actively learning Python and it is best that I write Python code daily as much as possible.

Read More

Breadth-First Search and Shortest Path in C# and .NET Core

A look at breadth-first search in C# for traversing graphs. Breadth-first search is very similar to depth-first search, except that it internally uses a Queue instead of a Stack when traversing the graph. In the case of an unweighted graph, breadth-first search can also find the shortest path between two vertices in a graph.

Read More

Depth-First Search Algorithm in C# and .NET Core

Program and use the depth-first search algorithm in C# and .NET Core to find all the vertices that can be reached in an undirected graph given a starting vertex. Depth-first search helps answer the question of connectivity, and is useful for mapping physical networks, web pages, social networks, etc. There are recursive and iterative versions of depth-first search, and this article will show the iterative version that requires an extra Stack Data Structure to maintain vertices visited.

Read More

Decorators in Python to Improve Naive Recursive Function for Fibonacci

I built my first decorator in Python to provide caching on Python functions that can support memoization, like the well-known recursive Fibonacci algorithm. Turns out a decorator, called lru_cache, already exists in functools that does memoization. This article shows how to create a custom Python decorator for caching as well as use the lru_cache decorator in functools to speed up the recursive Fibonacci algorithm.

Read More

Verify Binary Search Tree is Valid in Python

This weekend I completed the data structures portion of the Cracking the Coding Interview Questions on HackerRank. One of the programming challenges, validating a binary search tree, taught me how to verify a binary search tree is valid and I wanted to share my solution using Python.

Read More

Binary Heap and heapq in Python

I have been learning about Binary Heaps in my data structures class, and have been coding several examples in both Python and C#. In this article I want to introduce heapq in the Python collections module, which provide min Binary Heap functionality. I also provide an example of how to write a custom min Binary Heap in Python to perform Heapsort!

Read More

Queue Data Structure in Python and C#

Implementing a Queue Data Structure in Python and C#. In Python, I will describe how to use deque in the collections module as well as how to develop your own custom Queue using a Link List. In C#, I describe how to implement a Queue using an Array as well as using the Queue and Queue<T> Classes in System.Collections and System.Collections.Generic. I will also discuss abstraction in computer science to help hide the implementation details.

Read More

Local Functions and Memoization in Python to Solve for Fibonacci Numbers

Using memoization in the naieve, recursive algorithm for solving Fibonacci numbers makes a huge performance impact by reducing the number of recursive calls to re-solve Fibonacci numbers already calculated. In this case I will be using a Python dictionary and local functions ( nested functions ) to improve the recursive, naieve algorithm.

Read More

Stack Data Structure in Python and C#

This article first shows how to use the List Type in Python as a Stack data structure. It then provides an example of a custom Stack Class built in Python that still leverages the List Type in Python but uses abstraction to hide the underlying storage and its non-Stack methods. The article also shows how to use the deque in Python, Stack<T> in C# from System.Collections.Generic, and how to code the Stack data structure using a link list!

Read More

Inorder Traversal of Binary Search Tree in Python

In a recent programming challenge I was asked to code an inorder traversal of a binary search tree to print the values of its keys in the correct order. An order traversal of a binary search tree will print the keys of the node's left sub-tree, followed by the node's key, followed by the keys of the node's right sub-tree. I also show how to create a binary search tree in Python using level order traversal (like breadth-first search) and a list of ordered keys.

Read More

Bubble Sort in Python and HackerRank 30 Days of Code

Day 20 of HackerRank's 30 Days of Code was to write Bubble sort. I enjoy coding sorting algorithms (merge sort, insertion sort, selection sort, quicksort), so I was excited for the opportunity to write Bubble sort in Python, which I have done for fun in the past.

Read More

Quickselect Algorithm in Python

The Quickselect Algorithm is a selection algorithm based on the Quicksort sorting algorithm that provides a best case runtime of O(n) when finding the k-th smallest item in an unordered list. Quickselect achieves a better best case runtime compared to Quicksort, because it only has to recursively partition one side of the current partition. Here is an example of the Quickselect Algorithm using Python.

Read More

Set Host Environment in ASP.NET Core Using Command Line Arguments

Most articles talk about setting the host environment in ASP.NET Core using Environment Variables, but I wanted to set the host environment in an ASP.NET Core web application using command line arguments. This is actually really easy to do.

Read More

User Secrets in ASP.NET Core MVC Web Application

Learn how to store and retrieve user secrets in ASP.NET Core MVC Web Applications. This article discusses using the Secret Manager tool to add user secrets for your web application and retrieve them using ASP.NET Core Configuration. This is perfect for storing usernames and passwords for twitter, facebook, github, etc. per user outside of the project directory so they don't get pushed to your source code repository and shared with others.

Read More

Quicksort Algorithm in Python

Today, in one of my algorithms design and analysis classes I learned about Quicksort. Much like Merge Sort, Quicksort is a divide and conquer sorting algorithm that sorts the items in O(nlogn). In this article I will be writing a Quicksort function in Python.

Read More