Solving Common Data Structure Questions Using Python
By: Souvik Paul
Tags: Algorithms, Data Structures, Programming, Software Development, Python

According to Wikipedia, Data are the quantities, characters or symbols on which operations are performed by a computer which may be stored and transmitted in the form of electrical signals and recorded on magnetic, optical or mechanical recording media.

So, data needs to be managed in such a way so that it can produce some meaningful information. And that is why, we use data structures.

A data structure is the systematic way to organize data, so that it can be used efficiently.

Linear data structure — In this type of data structures, all the elements are arranged one after the other (i.e., sequential order). For example — Stack, Queue, linked lists, Arrays etc.

Non-Linear data structure —In this type of data structures, all the elements are not arranged in a sequential order. For example — Tree, Graph etc.

Static data structure — Here, the memory is allocated at the compile time. It provides faster access to the elements stored in it. But, the insertion and deletion is slow. For example — Arrays.

Dynamic data structure — Here, the memory is allocated at the run time. The insertion and deletion of elements is faster. But accessing the elements is slower. For example — Linked lists.

Now, we’ll solve some common data structure problems using python.

Problem 1

Write a program to find the number of trailing zeroes in N!.

For example:

5! = 120 (trailing zeroes = 1)

10! = 3628800 (trailing zeroes = 2)

Approach to the problem

First of all, we’ll calculate the factorial of the number using iteration. Then, we’ll divide the factorial by 10. If we get remainder as 0, we’ll then divide the factorial by 100. If, again, we get the remainder as 0, we’ll then divide the factorial by 1000. That is how we’ll divide the factorial with 10000, 100000, and so on, if we keep on getting remainder as 0. But if, at any time, we get remainder as non-zero, we’ll break out of the loop. So, the number of time the loop got executed will be the number of trailing zeroes.

Program

Outputs

Problem 2

Write a program for modular exponentiation.

` Result = (m^n)%p

For example:

(2³)%3 = 2

(5²)%2 = 1

Approach to the problem

The problem looks very simple to solve. But if we follow the traditional method, the value of x^y will grow very fast. To mitigate this problem, we’ll use a property.

For our case, we have (m^n)%p . So, it can also be written as -

Program

Outputs

Problem 3

You are given an array of integers from 1 to N in any order, that has one entry missing and you need to find it out with the help of a program.

For example:

array = [5,2,4,1] => Output = 3

array = [3,2,4,1,6] => Output = 5

Approach to the problem

Method 1: We’ll sort the array in ascending order. Then, we’ll iterate over the array to find the missing term.

Method 2: We can find the sum of the input array and then subtract it from the sum of first N natural numbers. The result will be the missing term.

Program (Method 1)

Output

Program (Method 2)

Output

Problem 4

Given an array of size N. Write a program to find the minimum number of operation required to convert the given array to ‘Palindromic Array’. The array [2,1,2] is a ‘Palindromic Array’ but [2,0,1] is not.

The only allowed operation is that you can merge first two elements of the array and replace them with their sum.

For example:

Input array : [1,2,3,1,6]

Operation1:

1+2 = 3 => array = [3,3,1,6] => Not in palindrome

Operation 2:

3+3 = 6 => array = [6,1,6] => Palindrome.

Output: Total operations = 2

Program

Output

Problem 5

Given an array of integers. Find the length of the longest subsequence such that elements in the sub-sequence are consecutive integers, the consecutive numbers can be in any order in the array.

For example:

Input: [3,2,4,1,7,10,6,11]

Output: Length of longest sub-sequence = 4

Approach to the problem

Input — [3,2,4,1,7,10,6,11]

Sort the array — [1,2,3,4,6,7,10,11]

Iterate over this array and check the difference between each two consecutive array. If it is 1, then copy the elements as it is to another array. If it is not 1, then insert a blank space.

New array — [1,2,3,4,’ ’,6,7,’ ’,10,11]

Now iterate over this new array and count the number of elements before the blank spaces and store them in a dictionary.

Then, find the largest value stored in the dictionary.

Output — 4

Program

Output

Problem 6

You are given an N x N 2D matrix. Write a program to rotate the matrix by 90 degrees (clockwise) without using an additional array.

For example: