Arrays and Strings Flashcards
How would you swap values in a list or an array? Used for reversing an array.
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 to reverse words in a string?
Can reverse the entire string and then reverse each word using two pointer technique
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)
Determine what is the starting point of a window (consecutive items) and then determine how to end that window
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
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 to find distinct values in a list
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; } }
Parentheses problem
Stacks and recursion
What does ArrayList set method do?
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 to copy a specific range of integers from an array?
Java:
Arrays.copyOfRange(arr, startIdx, endIdx)
C#:
Array.Copy(sourceArr, , destinationArr, , length)
If you have to do any sorting operation on array using O(n), what approach you should be targeting at
Try to see if you can use two pointer approach for that problem
Using base64 encode, a 6 letter long key could give you how many combinations
[64^6] possible strings
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
(arr[i] - ‘0’)
so if arr[i] was ‘1’, the above will give
(int)’1’ - (int)’0’ = 49-48 = 1
How to identify diagonals on a chessboard (or 2D matrix)
For each square on a given diagonal the (row - column) will be the same
How to then identify the anti-diagonal (diagonal orthogonal) on a chessboard (or 2D matrix):
For each square on an anti-diagonal the (row + column) will be the same
Jagged array (array of array) declaration:
int[][] x = new int[2][];
note that the index is empty in the RHS
Uniform 2D Array declaration:
int[,] x = new int[2,2];