The Interview was of duration 1 hour, The interviewer was very polite.
The stages of interview were:
private static int[] giveFrequency(String temp) {
int[] freq = new int[26];
for (int i = 0; i < temp.length(); i++) // O(S)
freq[temp.charAt(i) - 'a']++;
return freq;
}
private static String find_embedded_word(String[] list, String temp) {
int[] freq = giveFrequency(temp);
for (int i = 0; i < list.length; i++) {
int k;
int freqOfCurrentWord[] = giveFrequency(list[i]);
for (k = 0; k < 26; k++) { // O(1)
if (freq[k] < freqOfCurrentWord[k]) break;
}
if (k == 26)
return list[i];
}
return null;
}class Coordinate {
int x, y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
}
public static boolean found = false;
private static void dfs(char[][] arr, String str, int index, int i, int j, int r, int c, List<Coordinate> list) {
if (!found && (i < 0 || j < 0 || i >= r || j >= c || arr[i][j] == '*' || arr[i][j] != str.charAt(index)))
return;
list.add(new Coordinate(i, j));
char ch = arr[i][j];
arr[i][j] = '*';
if (index == str.length() - 1) {
found = true;
return;
}
if (!found)
dfs(arr, str, index + 1, i + 1, j, r, c, list);
if (!found)
dfs(arr, str, index + 1, i, j + 1, r, c, list);
if (!found)
arr[i][j] = ch;
if (!found)
list.remove(list.size() - 1);
}
private static List<Coordinate> find_word_location(char[][] arr, String str) {
found = false;
List<Coordinate> list = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
if (arr[i][j] == str.charAt(0))
dfs(arr, str, 0, i, j, arr.length, arr[i].length, list);
if (!list.isEmpty())
return list;
}
}
return list;
}It was a simple dfs but I was finding it hard where to stop the backtracking so I used if else with a static boolean like child :D.
Please upvote if it helps :)
Keep hustling