A SIMPLE DESIGN TEMPLATE FOR SOLVING PROBLEMS USING DATA STRUCTURES AND ALGORITHMS
Published: 2022-08-07 12:39:54

Hey Algorithmic thinker!

In this Article I'd highlight a simple design template for problem solving in algorithms by thinking through the problem.

The Design Template

This design template is actually a step or process to follow when facing any algorithmic problem whether in interviews, practice sessions , projects etc..

1. Constraints
2. Ideas
3. Complexities
4. Code
5. Testing

Constraints: This specifically focuses on the scope of the problem, this should basically answer the questions as regards the problem
An illustration is given below, Given the following arrays

L1 = [45,67,35,77,98,24,56], L2= [85,76,46,24,58,67,32], Find the common elements in both

Before taking any meaningful actions in solving the , you need to answer the following questions

i.) How large is the input of the given Array ? ( you might sometimes be given a max. Or minimum Input size to abide with )

ii.) What data type does the Array contain e.g. integer

iii.) Is the Data Sorted ?

iv.) What is the return type of the solution as soon as the problem is solved ?

v.) Is your response giving more than one match

After you’ve been able to answer the above questions for any algorithmic problem encountered, then you move to the next concept in the design template

2. Ideas: Ideas , helps you design solutions by

Simplifying the task
Thinking of suitable structures to use ( this is problem specific)
Think about related problems you know and how it was solved
3. Complexities

Solving problems is not just about a way of computing the right answer ( getting the solution) but the solution should work quickly enough and with reasonable memory storage/usage

An illustration is given below, Given the following arrays

L1 = [45,67,35,77,98,24,56], L2= [85,76,46,24,58,67,32], Find the common elements in both

I will highlight two different approaches to solve the above problem

i.)Brute force approach : this is an approach where a sample of the first set is used to search the other set to check for a match, it notes it down if there is a match and takes the second sample in the set and cross checks the other , this process continues like that until everything is searched. This process is very tedious and consumes a lot of space especially if the data size is large

L1 = [45,67,35,77,98,24,56] , L2 = [85,76,46,24,58,67,32]

The first data in the array L1 (45) will be used to sample every data in L2 for a match , and then the second data (67) goes through the same process and with every other set till the search is completed and any match is noted ( if any)

This is expressed as O( ln(L1) x ln(L2) )

ii.)Using a Hash Table : This involves creating an object by using a set.

A set of L1 and L2 which will create all unique elements of L1 and L2

Set(L1) => (45,67,35,77,98,24,56)

Set(L2) => (85,76,46,24,58,67,32)

And then overlap the two sets i.e. Look for the intersections of the two sets, Which give me my answer

This is expressed as O( min ln(L1), ln(L2) )

Looking at the two above solutions, it is clear that the second approach saves time and uses less memory which is why it’d chosen as a solution to the algorithmic problem

4. Code

This is dependent on the choice of programming language you’re familiar with e.g. Python, java, C+, javascript etc.

The following should also be noted

You can write first in pseudocode , before coding on your IDE
Decompose your code into small logical pieces
Read our code to yourself at every step
5. Testing

You can always test for the following in your solutions

Edge Cases : This defines the extreme cases and it is based on the constraints( Problem)
Cases where there are no solutions
Non-trivial functions test to test if your algorithm works correctly.
This design template when followed religiously will give a better edge when solving Algorithmic problems.

Thanks for reading