cover-img

Required programming techniques for DSA

Having a solid understanding will definitely improve the problem solving skills

6 February, 2023

12

12

1

We apply many programming and problem-solving techniques to write an effective implementation of a data structure or an algorithm. Some of these are fundamental ones that are frequently used and can be found in any programming language.

Not having enough practice covering these techniques is one of many reasons why people find DSA hard later on.

What will we cover?

  • Fundamental programming techniques
  • Importance to DSA
  • Code examples in Java

Primitives

A primitive is a fundamental data type. Numbers, characters, and booleans are considered primitives. They can be used individually. They can also be grouped to form a custom data type.

Why are they important?

  • Primitives are the fundamental data units of any data structure.
  • Most operations performed on a data structure are ultimately carried out by primitives. E.g., sorting, searching, etc.
  • The problems on these primitives range from very simple to complex in nature. Solving them will improve our problem-solving skills and help in writing complex algorithms.
int age = 18;
char errorCode = 'E';
boolean isBlocked = true;

Variables and Values

Let’s take this example

Today’s stock price of ABC Holdings is $123

We can dissect the entire statement into two parts.

  1. Today’s stock price of ABC Holdings
  2. $123

The first one is variable in nature. The actual value of it may change with time.

At the same time, the second one is static data. It’s just a value. Here the value is primitive data. It can also be a complex type of data.

💡 Many programming languages require programmers to define the variable's data type before assigning a value to it. These languages are called static-typed. e.g., C, C++, Java, etc. Dynamic-typed languages like Python and JavaScript allow storing any type of data in a variable. The data type of a variable is detected at runtime.

Why is it important?

  • A data structure will contain a lot of data kept structured. To maintain the structure, we require variables. These variables will hold the data (value).
  • Algorithms are blueprints of operations to be carried out on data. In this process, we will require a lot of variables holding intermediate results.

Operators, Expressions, and Statements

What will we do with a piece of value? Maybe we can carry out some operations. All programming languages provide a basic set of operators to carry out some fundamental operations. These operators are mostly optimized and independently carry out an operation.

Let’s take this example.

John had 5 apples in his basket. He picked 3 more and kept those in his basket.

Operation

John added 3 apples. So, it’s an addition operation.

Operator

+ is the operator used for the addition operation.

Expression

Let’s say appleCount is the variable we defined to hold the value for the number of apples we have in the basket. So, this will be the expression.

appleCount + 5

Statement

We just increased the count. But did we assign it to update the existing count?

appleCount = appleCount + 5;

Why are they important?

An expression in its simplest form is just a variable or an operation we carry out on variables. A statement lets us combine multiple expressions. An algorithm contains multiple statements.

What to look for?

  • Assignment operations are going to be one of the most frequently used operations. We just saw how we can assign a value or the result of an expression to a variable.
  • Apart from it, some of the most frequently used operations are arithmetic, comparison, and logical operations. Let’s learn about those in the following sections.

Arithmetic Operations

Why are numbers so important? Let’s try to answer that by asking one question. How many times in a normal day of your life do you come across numbers? See, even this question will have an answer in the form of a number itself.

Lisa turns 3 today.

Farah lost a teeth.

I invested $50.

Tara made a profit of $1000 in just a month.

She drinks half a litre of milk every morning.

That building is of 10 floors.

Sita is tall, 187cm.

When we have numeric data with us, the most fundamental operations we will be carrying out on them are the arithmetic operations. Almost all programming languages provide operators for addition, subtraction, multiplication, division, modulus, and exponentiation operations.

Comparison Operations

Comparison operations can be carried out naturally on numbers and characters. We can also write algorithms to perform on other data types. Some examples:

Compare students based on their semester marks

Compare developers based on number of bugs assigned to them

Compare cities based on their pollution metric index

Why is it important?

  • One of the most predominant reasons (arguably) for using comparison operations is arranging items in an order.
  • When items are kept in order, it’s easy (and quick) to find, add, and remove. Many data structures internally arrange their elements in a particular order.

What are different comparison types?

Basically, there are 3 natural ways comparisons are made: Equality, Greater, and Smaller. But various combinations can be made for a fine-grained check.

  • Equal to
  • Not equal to
  • Greater than
  • Greater than or equal to
  • Smaller than
  • Smaller than or equal to

What does a comparison operation return?

When two items are compared, what is the output? Think logically. Yes. They return a boolean value. Some languages consider it 0 (zero) and non-zero. Some consider two special boolean values: true and false. This will be very important in the logical operations (that we will discuss next).

Logical Operations

Arithmetic operations are performed on numbers. Numbers and characters can be part of comparison operations. What about boolean values? What operations can be carried out on them? Yes. Logical operations.

Let’s understand it using some examples.

Check if a student is a girl and secured more than 75 in Mathematics.

31st is the due date. Check if there is any due and it’s paid.

Summer is the best time to visit Manali. Check if vacation has started and flight tickets are below ₹10K.

Why is it important?

Logical operations like comparison operations return boolean values. These boolean values can be used as conditions. Based on a condition, we may decide to execute or skip some statements. Sometimes we may execute some statements until a condition is met.

What are the different logical operations?

There are 3 fundamental logical operations. Almost every programming language supports them.

  1. NOT: It returns the opposite boolean value.
  2. AND: It returns true only when all participating conditions are true.
  3. OR: It returns false only when all participating conditions are false.

Conditions

A condition is a boolean value that’s returned by a comparison or logical operation. Based on a condition, we may decide which statements to execute or skip.

Let’s understand it using some examples.

If an Uber driver agreed for a ride, call your mom.

If “Black Friday Sale” is going, let’s visit the mall.

Why is it important?

A program runs through inputs. It’s highly unlikely that the inputs will always remain the same. When inputs vary, the execution will also vary, resulting in a different output.

What are the different ways to execute conditions?

Different programming languages may have different ways of handling conditions. But the below are supported by almost all languages (syntax may vary)

  1. if…else: if a condition matches, execute something. If not, execute something else.
  2. switch…case: when we have different match conditions for a single input
  3. try…catch: When a program encounters an unexpected input, it may halt the execution. In modern programming, it’s handled using try…catch.

Loops

A loop allows the execution of the same set of statements repeatedly. There may be a counter or some other predefined condition that will determine the number of times the loop will execute.

Let’s understand it using some examples.

The sprinter ran 400m for 4 times.

My mom makes 10 rotis every day.

Verify each page in the answer sheet until there is a blank page.

Why is it important?

  • Looping allows refactoring. We won’t be writing the same line of code for each execution.
  • A data structure holds the similar type of elements. We may loop through all these elements to carry out important operations like traversing, inserting, deleting, etc.
  • Looping is very important for iterations. For an algorithm where iterations are required, we will use a looping mechanism. e.g., the sum of all numbers in an array.

What are the different ways of executing loops?

  1. while: Execute a set of statements till a condition is matched. The condition is matched before each execution of looping statements.
  2. for: It’s similar to a while loop. The difference is that it allows some variables to be initialized and updated before each execution of looping statements.
  3. do…while: It’s similar to a while loop. The only difference is the condition is matched after each execution of looping statements. Even if the condition isn’t matched, the statements will be executed at least once.

Functions

Like a loop, functions are also meant for the refactoring of code. If a group of statements are so common and may get executed from multiple places in a program, it’s the best thing to bring them inside one function. A function allows a group of statements to be executed from different parts of the program.

Let’s take some examples.

Opening a file.

Writing logs to a log file.

Draw a bar chart.

As you may guess, a function may be a little abstract. It means a function may expect some inputs. Its execution may be dependent on these inputs.

Why is it important?

  • In recursions, a function calls itself (of course with different parameters).
  • In divide ‘n’ conquer algorithms, a function calls itself with a chunk from the original data. Later it combines all the results.
  • In dynamic programming, we can store (and cache) the outputs of some function calls.

Pointers or References

Pointers are the variables that hold the memory address of another variable. In C and C++, they are used extensively. Pointers are powerful and dangerous.

Modern programming languages introduce the concept of references. A reference is like an alias. It’s like multiple variables in a program are pointing to the same memory address. References could be restrictive but easy to manage.

Why is it important?

In data structures like Linked List, Tree, and Graph, each element is stored inside a node. Each node may connect to one or more nodes.

A few more…

Arrays

Once we are done with primitive types, the next step should be learning Arrays. Arrays allow us to store similar types of data together in contiguous memory.

Let's understand it using some examples.

List of companies owned by Mr. X

Students who participated in the coding hack.

Strings

Strings are sometimes considered an array of characters. But it’s more than that. Strings are so common in a program that many programming languages treat them like a primitive.

Objects

We can compose an object by combining all primitives, arrays, strings, etc. Even another object can be a part of it. The behavior (allowed actions) of an object can be added. An object in programming closely resembles an object in the real world. e.g., a human, an answer sheet, performance metrics of an employee, etc.

Variable Scope

While using functions, a very important thing to understand is the scope of the variables. Variables that can be used inside any function are called global. Local variables can be accessed inside the function where they are defined.

Final Words

Data structures and algorithms involve a lot of programming techniques. So it’s very important to have a solid foundation. The best approach would be to practice a sufficient number of problems related to these techniques.

I have written another show that highlights important things to take care of in order to be successful in DSA. Read it here.

programming

algorithms

problemsolving

datastructures

12

12

1

programming

algorithms

problemsolving

datastructures

Swapna Kumar Panda
20+ years in Software Engg | Currently writing and teaching DSA/JavaScript/React/Python | Active content writer

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.