Arrays and Strings Flashcards

1
Q

How would you swap values in a list or an array? Used for reversing an array.

A

Two approaches to reverse:
1. By creating a new array of same size then starting from the end populate this new array. Drawback is we get space complexity of O(N) along with time O(N)
2. In-place technique
Two pointer technique in which you use two pointers either you use it to exchange/swap values. You remove the space complexity and time is pretty similar O(N/2) ~ O(N)

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

How to reverse words in a string?

A

Can reverse the entire string and then reverse each word using two pointer technique

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

How to find max or min number of items in an array that appear consecutively?

Example:
Input: [1, 1, 2, 2, 2]
Output : 3 (2 occurs thrice)

A

Determine what is the starting point of a window (consecutive items) and then determine how to end that window

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

How to find the integer that occurs once in an array of integers in which there are exactly two occurrences of every value except for one?

Example:
Input: [2,2,1]
Output: 1

A

Hashmap can be using to keep track of counts

In ONLY this scenario where there are exactly TWO occurrences we can do bit manipulation (XOR)

class Solution {
  public int singleNumber(int[] nums) {
    int a = 0;
    for (int i : nums) {
      a ^= i;
    }
    return a;
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to find distinct values in a list

A

Use one slow pointer and other faster. move the faster pointer till you find a non distinct value and then swap with the value that repeats

class Solution {
    public int removeDuplicates(int[] nums) {
        if (nums.length == 0 || nums.length == 1)
          {
             return nums.length;
          }
        int i =0;
        for (int j = 1; j< n; j ++) {
           if (nums[i] != nums[j]) {
               i++;
               nums[i] = nums[j];
           } 
       }
        return j+1;
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Parentheses problem

A

Stacks and recursion

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

What does ArrayList set method do?

A

public E set(int index, Element E)

If there is a need to update the list element based on the index then set method of ArrayList class can be used. The method set(int index, Element E) updates the element of specified index with the given element E.

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

How to copy a specific range of integers from an array?

A

Java:
Arrays.copyOfRange(arr, startIdx, endIdx)

C#:
Array.Copy(sourceArr, , destinationArr, , length)

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

If you have to do any sorting operation on array using O(n), what approach you should be targeting at

A

Try to see if you can use two pointer approach for that problem

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

Using base64 encode, a 6 letter long key could give you how many combinations

A

[64^6] possible strings

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

if we have a string or arr of chars arr that are int values, like “0000”, “1100”, ‘0’ then how to convert char into an int

A

(arr[i] - ‘0’)

so if arr[i] was ‘1’, the above will give
(int)’1’ - (int)’0’ = 49-48 = 1

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

How to identify diagonals on a chessboard (or 2D matrix)

A

For each square on a given diagonal the (row - column) will be the same

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

How to then identify the anti-diagonal (diagonal orthogonal) on a chessboard (or 2D matrix):

A

For each square on an anti-diagonal the (row + column) will be the same

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

Jagged array (array of array) declaration:

A

int[][] x = new int[2][];

note that the index is empty in the RHS

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

Uniform 2D Array declaration:

A

int[,] x = new int[2,2];

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