Monday 11 January 2021

Cross cutting concerns in Clean architecture

Handling cross cutting concerns in application code can become cumbersome if right approach is not utilized. Imagine dealing with caching, logging, authentication, authorization etc. in your core application code wherein those blocks of code bear no core content. Cross cutting concerns are best handled as "aspects" and should be configured/attached as behavior to the host/infrastructure instead of core application code. We tried to explain the same concept in a video. Hope you like it.


Find Kth Largest Number in an Array : Python, C#, Java

Continuing on the journey to understand the strength and weakness of languages when solving algorithm problems, let us look at the leetcode problem of finding kth largest number in an array.

In essence, the most common way to address any algorithm problem that talks about kth largest or kth smallest element in an stream or an array, is to utilize a priority queue that keeps the internal set of elements in sorted order (insert would take log(k) where k is the size of priority queue) and lets you remove the min or max element as O(1) operation. There are different implementations of priority queues available in different languages - most typical one is using a heap (min or max). Here is a quite useful thread that talks about heaps and priority queue.

Back to our problem. Below are possible solutions in C#, Python and Java. I did not attempt JavaScript as it does not have a built in type of priority queue; you could implement it though (reference).

Python (heapq makes it easy)

def findKthLargest(self, nums: List[int], k: int) -> int:

        pq = []

        for i in range(len(nums)):

            heapq.heappush(pq, nums[i])

            if(len(pq) > k):

                heapq.heappop(pq)

        return pq[0]

C# (No in-built data type. Can use SortedList which is close to a minHeap but you need to manage duplicate elements on your own)

public int FindKthLargest(int[] nums, int k) {

        SortedList<int,int> sortedList =new SortedList<int,int>();

        int itemCount=0;

        foreach(int num in nums)

        {

            if(sortedList.ContainsKey(num))

                sortedList[num] += 1;

            else

                sortedList[num] = 1;

            itemCount++;

            if(itemCount > k)

            {

                if(sortedList[sortedList.Keys.First()]==1)

                    sortedList.Remove(sortedList.Keys.First());

                else

                    sortedList[sortedList.Keys.First()]--;

                itemCount--;

            }

        }

        return sortedList.Keys.First();

    }

Java (use in-built PriorityQueue type)

public int findKthLargest(int[] nums, int k) {

        PriorityQueue<Integer> heap = new PriorityQueue<>();

        for(int num: nums){

            heap.offer(num);

            if(heap.size() > k){

                heap.poll();

            }

        }

        return heap.peek();

    }

Observations

1. Since Java and Python have in-built support for priority queue, you can simply focus on logic. Since we are trying to utilize a makeshift type in C#, we have to deal with more than just logic to write the correct code.

2. Test results from leetcode submission. Java consumed highest amount of memory but took the least amount of time. Python produced a balanced result though.




Tuesday 5 January 2021

Clean Architecture in Azure Functions - Introduction

Clean Architecture practices let you build the system in such a way that its core remains independent of a hosting environment and you can easily makes changes to the system while remaining agile. There is plenty of content on the internet that explains the details of this practice in much better way than what I can do e.g. here and here.

Here is a humble attempt from me and couple of my friends to showcase the benefit of such practices in the world of Azure Functions. Below is the video from our YouTube channel.


Thank you for your support !!

Sunday 3 January 2021

house-robber: C#, Python, JavaScript, Java

Continuing on the experience of learning the pros/cons for different programming languages, let us try to solve the leetcode problem titled house-robber.

The algorithm is straightforward once you understand it. Essentially, you need to apply dynamic programming technique and dynamically decided on each house whether to take money from the house or leave it in order to maximize the outcome abiding by the condition that you can not take money from 2 consecutive houses.

psuedo code:

    if there is 1 house, take money from it.

    if there are 2 houses, take money from house that has more money

    for ith house, take the higher of the 1. money collected till i-1th house 2. money     collected till i-2th house and money present in ith house

C#

    public int Rob(int[] nums) {

        if(nums == null || nums.Length == 0){

            return 0;

        }

        if(nums.Length == 1){

            return nums[0];

        }

        if(nums.Length == 2){

            return Math.Max(nums[0], nums[1]);

        }

        var dp = new int[nums.Length];

        dp[0] = nums[0];

        dp[1] = Math.Max(nums[1], nums[0]);

        for(int i = 2; i < nums.Length; i++){

            dp[i] = Math.Max(dp[i-1], dp[i-2] + nums[i]);

        }

        return dp[nums.Length - 1];

    }

JavaScript

var rob = function(nums) {

    if(nums === undefined || nums.length == 0){

        return 0;

    }

    if(nums.length == 1){

        return nums[0];

    }

    if(nums.length == 2){

        return Math.max(nums[0], nums[1]);

    }

    const dp = new Array(nums.length);

    dp[0] = nums[0];

    dp[1] = Math.max(nums[0], nums[1]);

    for(let i = 2; i < nums.length; i++){

        dp[i] = Math.max(dp[i-1], dp[i-2] + nums[i]);

    }

    return dp[nums.length - 1];

};

Python

def rob(self, nums: List[int]) -> int:

        if not nums :

            return 0

        length = len(nums)

        if length <= 2 :

            return max(nums)

        dp = [0]*length

        dp[0] = nums[0]

        dp[1] = max(nums[0], nums[1])

        for idx in range(2, length, 1):

            dp[idx] = max(dp[idx - 1], dp[idx - 2] + nums[idx])

        return dp[length - 1]

Java

public int rob(int[] nums) {

        if(nums == null || nums.length == 0){

            return 0;

        }

        if(nums.length == 1){

            return nums[0];

        }

        if(nums.length == 2){

            return Math.max(nums[1], nums[0]);

        }

        int[] dp = new int[nums.length];

        dp[0] = nums[0];

        dp[1] = Math.max(nums[0], nums[1]);

        for(int i = 2; i < nums.length; i++){

            dp[i] = Math.max(dp[i-1], dp[i-2] + nums[i]);

        }

        return dp[nums.length - 1];

    }


Observations:

1. Initializing an array of a certain length is little weird in Python (but intuitive at the same time) where you just multiply [0] n times :)
2. Python code consumes the least lines of code when writing identical code in different languages. It is possible to reduce the lines of code in all languages by getting rid of dp array and using 2 pointers as at every ith position, one is only interested in i-1 and i-2 values. However, i believe python code will still be the most succinct. Take below code for example that swaps 2 variables.

C#, Java, JavaScript
int tmp = val1;
val1 = val2;
val2 = tmp;

Python
val1, val2 = val2, val1