All posts by year

Showing everything matching: clear

2023

How to parameterize Azure API management policy files when using Bicep

18 Feb 2023
Azure Bicep API management

I recently started working with Bicep to setup deployments of APIs to Azure. Bicep modules provide an easy-to-manage process when single-API deployment is needed.

More...
2022

How to run ASP.NET Core eShopWeb sample in a local Kubernetes cluster

21 Aug 2022
Kubernetes ASP.NET Core Rancher Desktop

Lately, I’ve been learning Kubernetes, arguably the most popular container orchestrator. Since I’m a .NET developer, I thought it would be helpful to try out what I’d learned so far by hosting the eShopOnWeb project on my local Kubernetes cluster. Let’s walk through this together.

More...

How to run ASP.NET web application on Service Fabric using a docker container (Part 1)

04 Jan 2022
Docker Service Fabric ASP.NET IIS

I have recently been working on a project at work to migrate an Umbraco v7 on-prem service to Service Fabric. Typically, we configure our Service Fabric applications using self-hosted OWIN. However, Umbraco heavily utilizes HttpContext in their code, which is unavailable in the OWIN context, therefore this approach did not work.

Luckily, Service Fabric supports running docker containers in a highly available, distributed, and resilient environment. It is relatively simple to set up since Service Fabric provides most of the configuration needed to run the containers. Therefore I was able to take the existing WebAPI project, containerize it, and run it in Service Fabric.

I’ll walk you through how to get a Web API up and running in a local Service Fabric cluster within a container.

More...
2021

Swap Nodes In Pairs

16 Jul 2021
Linked lists Recursion Code challenge

It’s been a while since I looked at some coding challenges, so I decided to tackle a fun problem. The problem can be found in Leetcode, one of the most popular coding practice platforms. It has a ton of problems, majority of which have been asked in technical interviews at top tech companies like FAANG (Facebook, Amazon, Apple, Netflix, Google), and Microsoft.

Here’s the problem statement:

Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list’s nodes (i.e., only nodes themselves may be changed.)

Example

The problem states that we cannot modify the values of the list nodes themselves, otherwise the solution would be too easy! We also, of course, cannot just create a new auxiliary list. Creating an auxiliary list would incur O(n) space complexity. We must swap the nodes in the linked list given, i.e. O(1) space complexity.

Two ideas on how to solve this come to mind:

  1. Iterative: Consider each pair and rewire the second node’s next pointer to point to the first node. Maintain a previous and a next pointer reference in order to stitch nodes together
  2. Recursive: Recurse through each pair, each time passing the first node of the next pair, until you get to the end, then rewire nodes as you traverse the call stack.

We need to be careful about odd numbered lists e.g. 1 -> 2 -> 3, as well as making sure we do not introduce cycles in the list as we rewire nodes.

Below is my iterative and recursive solutions in Java, with comments to explain what’s going on:

Iterative

ListNode swapPairsIterative(ListNode head) {
    // maintain a dummy node since the head will change when we swap nodes
    ListNode dummy = new ListNode();
    ListNode prev = dummy;
    dummy.next = head;
    ListNode curr = head;
    while (curr != null && curr.next != null) {
        // store reference to next pair
        ListNode next = curr.next.next;
        // store reference to the node that will be first in current pair
        ListNode newCurr = curr.next;
        // swap pair nodes
        curr.next.next = curr;
        // dereference next pointer to avoid cycles
        curr.next = null;
        // rewire previous pair with current pair
        prev.next = newCurr;
        prev = curr;
        curr = next;
    }
    
    // handle tail of odd numbered lists
    if (curr == null || curr.next == null) prev.next = curr;
    
    return dummy.next;
}

Recursive

ListNode swapPairsRecursive(ListNode head) {
    // base case
    if (head == null || head.next == null) return head;
    ListNode nextPairHead = swapPairsRecursive(head.next.next);
    // swap pair nodes
    ListNode newPairHead = head.next;
    head.next.next = head;
    // dereference next pointer to avoid cycles
    head.next = null;
    // rewire previous pair with current pair
    head.next = nextPairHead;
    return newPairHead;
}

As you can see the recursive approach a bit more cleaner, but the iterative is not too bad either. Both have O(n) time complexity since we’re visiting each node once, and O(1) space complexity since we’re modifying the list in-place.

Thanks for reading, and happy coding!

More...

Generating Storage Account SAS Token in Linked ARM Templates

04 Jul 2021
Azure ARM Templates

One of the main challenges when setting up Linked ARM templates is how to store the templates so that they are accessible by ARM without making them completely public. Microsoft recommends storing the templates in an Azure Storage Account and securing them with a SAS token. Shared Access Signature (SAS) tokens provide secure access to Azure Storage Account. The scope of the access can be limited to the account, containers, or objects. You can read more about SAS tokens here.

More...

How I built this blog site with Jekyll, Docker-Compose, and Github Pages

27 Jun 2021
Jekyll Docker-Compose Github Pages

It started with me hoping to start a blog site because I thought it would be cool to have one. However, I didn’t want to go through the hassle of setting up a web application to host the site, which would need a database to host the content. After some digging, I came across Jekyll. Setting up a blog site using Jekyll is a piece of cake! All the posts are just markdown files (text files on steroids), and you can get up and running with a curated prebuilt assortment of themes. Kudos to the creators of Jekyll!

More...

Subscribe to receive notifications about new blog posts by email

* indicates required