Data Structure And Algorithm MUST KNOW PROBLEMS (Uses Java as example language) Flashcards

All the most common LeetCode and HackerRank Interview questions, and their related concepts such as data structures and Big O

1
Q

How many ASCII Characters are there?

Now, write an algorithm for populating an array with every character (use Java as example language)

A
There are 256 ASCII characters.
//Java code to print every char in order:
int i = 0;
while(i <= 255) {
    System.out.println((char)i + " = " + i);
    i++;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the formula for radial opposite? This is the opposite of a given integer on circle of m integers along its circumfrence. Eg: if circle is 10 in circumfrence, and provided i = 1 as input, then output 6.

A

import java.util.*;

 class Dcoder
 {
   public static void main(String args[])
   { 
    Scanner sc = new Scanner(System.in);
    String s1 = sc.next();
    String s2 = sc.next();
    int x = Integer.parseInt(s1);
    int y = Integer.parseInt(s2);
    int opposite = ((x/2)+y)%x; 
    System.out.println(opposite);
   }
 }

Answer: ((x/2)+y)%x;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the distance formula for a coordinate grid? Convert this statement to code in Java.

Hint: Distance formula is the distance from any one point to another on a coordinate grid.

A

Math.sqrt((Y2-Y1)^2 + (X2-X1)^2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Formula to find all prime numbers up to N:

Recommendation: Make an isPrime method to handle the inner loop.

A
class Dcoder {
   public static void main(String args[])   { 
    Scanner sc = new Scanner(System.in);
    int M = Integer.parseInt(sc.next());
    int N = Integer.parseInt(sc.next());
    while(M <= N) {
      if(isPrime(M)) {
        System.out.println(M);
      }
      M++;
    }
   }
   static boolean isPrime(int M) {
     if(M ==0 || M == 1) {
       return false;
     }
     int k = 2;
     while(k < M) {
       if(M%k == 0) {
         return false;
       }
       k++;
     }
     return true;
   }
 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

FizzBuzz

A

a

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Print all Primes

A

p

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Fibbonacci

A

f

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Remove Duplicates

A

r

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Minimum USD Coin Change

A

c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Remove the Nth from the last node in a linked list.

A

r

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Reverse a Linked List

A

r

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Minimum Coin Change with ANY Coins

A

m

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Convert Roman numerals to decimal

A

c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Shortest Distance to Char in a string

A

s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Shift an array left by d shifts

A

s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Determine whether two strings are permutations of each other

A

p

17
Q

Maximum Sub Array (Kadane’s Algorithm)

A

s