Topic refresher ( DSA 1 - 5 )

Modified on Tue, 28 Mar, 2023 at 4:58 PM

Common to DSA - 1 to DSA - 5

How do I dry-run my code?

Come up with base cases and edge cases (input and expected output). Take one test case at a time and use pen and paper or an editor to step through each line of code and note down values. See if your code can give you the expected output.
You can also try to run your code with these inputs and use print statements to see the modification of values when your code executes.  This might be a time-consuming process but it will help you find out where your code is failing. 


DSA - 1


What are co-prime numbers and how do you find GCD?

A pair of numbers are said to be coprime when they have their highest common factor(HCF) as 1. If we want to check if two numbers are co-prime or not we have to find the Greatest common divisor (G.C.D) of the two numbers and check if it's 1 or not.

THe Euclidean algorithm can be used to find the greatest common divisor (GCD) of two positive integers n1 and n2. It takes n2 as the modulus of n1 and the previous value of n2 until n2 becomes 0. The GCD is then returned as n1.

Resource: How to find GCD


How do I create a heap in JavaScript?

We can create a Heap to get the minimum number or maximum number from a set of numbers in O(1) time.


JavaScript does not come with a collections library so we would have to implement our own heap data structure using an array, another option is to install a collections package using npm but this is not possible when doing the problems in the Crio workspace. 

We will have a resource in the problem description that implements a heap using array methods which you can use to solve the problem.

Resource: Efficient way to implement Priority Queue in Javascript


How do we create an Object array in java?

The main advantage of Object array is that it can store objects, so we can store a string and an int in the same array if required.

Creating an Object array in java is the same as creating any other array in java. We can do it like this: Object[] <Array name>= new Object[<size>];

Object[] exp1= new Object[2]; //Creates empty array

Object[] exp2 = {‘a’, 1}; //Creates a array with the given string and int values


DSA - 2

How does the java comparator work?

The Comparator interface is used to order objects of a user-defined class in the java.util package. It contains two methods, compare and equals, and allows sorting elements based on various data members such as roll number, name, age, etc. The sort method of the Collections class can be used to sort the elements of a List based on a Comparator. To sort a List, the ComparatorClass must implement the Comparator interface.

The idea is that the data structure (or sorter) calls the comparison function any time it needs to order two elements, to find out what order to put them in. We put (-1, 0, 1) in return based on the condition to define the order, instead of returning the (-1, 0, 1), we can just return the difference between two values which also makes the code compact. 


import java.util.Comparator;

public class PersonComparator implements Comparator<Person> {

    @Override

    public int compare(Person person1, Person person2) {

        return person1.getName().compareTo(person2.getName());

    }

}

In this example, the Comparator interface is implemented by the PersonComparator class. The compare method compares two Person objects based on their names. If the name of person1 is lexicographically less than the name of person2, a negative integer is returned. If the names are equal, zero is returned, and if the name of person1 is lexicographically greater than the name of person2, a positive integer is returned.


DSA - 3

How to implement a stack in JS?

You can use an array with push() and pop() to implement a stack using array.

st = []

st.push(2) //push 2 to stack

st.push(3) //push 3 to stack 

st.pop() //pops the top of stack, in this case 3 will be popped 


How to implement a Stack and Queue in Python?

Lists can be used to implement a stack and queue in python, you can use append() and pop() to implement a stack and similarly use append() and pop(0) to use it as a queue. Python also has queue and deque packages which you can import and use.  

Stack:

St = []

st.append(10) # push 10 on top 

st.pop() # pop the top element 


Queue:

from collections import deque # importing the deque

q = deque() # creating an object for deque

q.append(10) # enqueue 10

q.popleft() # dequeue


DSA - 4

Why mod with 1000000007?

Values are modded with 1000000007 because it's the largest prime number that is under the int range.  It being a prime number is crucial because Prime numbers are not divisible by any other number.


DSA - 5

What is XOR?

XOR is a binary operation, it stands for "exclusive or", that is to say, the resulting bit evaluates to one if only exactly one of the bits is set.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article