"Traverse the array of points while computing the distance and pushing it to the heap. Then traverse again the heap and pop the k closest. Time O(nlogn) Space O(n)"
Dadja Z. - "Traverse the array of points while computing the distance and pushing it to the heap. Then traverse again the heap and pop the k closest. Time O(nlogn) Space O(n)"See full answer
"First thing the interviewee did wrong is not asking clarifying questions. This is the most vague problem I have every heard, and the interviewee just made assumptions and started programming."
Nicholas S. - "First thing the interviewee did wrong is not asking clarifying questions. This is the most vague problem I have every heard, and the interviewee just made assumptions and started programming."See full answer
"we can use two pointer + set like maintain i,j and also insert jth character to set like while set size is equal to our window j-i+1 then maximize our answer and increase jth pointer till last index"
Kishor J. - "we can use two pointer + set like maintain i,j and also insert jth character to set like while set size is equal to our window j-i+1 then maximize our answer and increase jth pointer till last index"See full answer
"A recursive backtracking solution in python.
def changeSigns(nums: List[int], S: int) -> int:
res = []
n = len(nums)
def backtrack(index, curr, arr):
if curr == S and len(arr) == n:
res.append(arr[:])
return
if index >= len(nums):
return
for i in range(index, n):
add +ve number
arr.append(nums[i])
backtrack(i+1, curr + nums[i], arr)
arr.pop()
"
Yugaank K. - "A recursive backtracking solution in python.
def changeSigns(nums: List[int], S: int) -> int:
res = []
n = len(nums)
def backtrack(index, curr, arr):
if curr == S and len(arr) == n:
res.append(arr[:])
return
if index >= len(nums):
return
for i in range(index, n):
add +ve number
arr.append(nums[i])
backtrack(i+1, curr + nums[i], arr)
arr.pop()
"See full answer
Software Engineer
Data Structures & Algorithms
+1 more
🧠 Want an expert answer to a question? Saving questions lets us know what content to make next.
"Idea for solution:
Reverse the complete char array
Reverse the words separated by space. i.e. Find the space characters and the reverse the subarray between two space characters.
vector reverseSubarray(vector& arr, int s, int e)
{
while (s reverseWords(vector& arr )
{
int n = arr.size();
reverse(arr, 0, n - 1"
Rahul M. - "Idea for solution:
Reverse the complete char array
Reverse the words separated by space. i.e. Find the space characters and the reverse the subarray between two space characters.
vector reverseSubarray(vector& arr, int s, int e)
{
while (s reverseWords(vector& arr )
{
int n = arr.size();
reverse(arr, 0, n - 1"See full answer
"Let me try to explain it with simple life analogy
You're cooking dinner in the kitchen. Multithreading is when you've got a bunch of friends helping out. Each friend does a different job—like one chops veggies while another stirs a sauce. Everyone focuses on their task, and together, you all make the meal faster.
In a computer, it's like different jobs happening all at once, making stuff happen quicker, just like having lots of friends helping makes dinner ready faster."
Praveen D. - "Let me try to explain it with simple life analogy
You're cooking dinner in the kitchen. Multithreading is when you've got a bunch of friends helping out. Each friend does a different job—like one chops veggies while another stirs a sauce. Everyone focuses on their task, and together, you all make the meal faster.
In a computer, it's like different jobs happening all at once, making stuff happen quicker, just like having lots of friends helping makes dinner ready faster."See full answer
"Average case - lookup/insert/delete - o(1) -> assuming a low load factor and uniform hash distribution.
Worst case - o(n) -> where are keys collide in same bucket"
Kargi C. - "Average case - lookup/insert/delete - o(1) -> assuming a low load factor and uniform hash distribution.
Worst case - o(n) -> where are keys collide in same bucket"See full answer
"You can ask some clarifying questions like
1) Ask if the list is already sorted or not
2) is zero included in the list ?
3) Natural numbers are usually positive numbers ( clarify they are non negatives)
Solution :
1) If sorted use two pointers and sort them in O(N)
2) if not sorted , -ve / only +ve numbers in the list doesn't matter - the easiest solution is
Use a priority queue and push the number and its square in each iteration
Finally return the list returned by the priority Queue. N"
Bless M. - "You can ask some clarifying questions like
1) Ask if the list is already sorted or not
2) is zero included in the list ?
3) Natural numbers are usually positive numbers ( clarify they are non negatives)
Solution :
1) If sorted use two pointers and sort them in O(N)
2) if not sorted , -ve / only +ve numbers in the list doesn't matter - the easiest solution is
Use a priority queue and push the number and its square in each iteration
Finally return the list returned by the priority Queue. N"See full answer
"public class CircularBuffer {
private T[] buffer;
private int head;
private int tail;
private int size;
private final int capacity;
public CircularBuffer(int capacity) {
this.capacity = capacity;
this.buffer = (T[]) new Object[capacity];
this.head = 0;
this.tail = 0;
this.size = 0;
}
public void enqueue(T item) {
if (isFull()) {
throw new IllegalStateException("Buffer is full");
}
buf"
Vidhyadhar V. - "public class CircularBuffer {
private T[] buffer;
private int head;
private int tail;
private int size;
private final int capacity;
public CircularBuffer(int capacity) {
this.capacity = capacity;
this.buffer = (T[]) new Object[capacity];
this.head = 0;
this.tail = 0;
this.size = 0;
}
public void enqueue(T item) {
if (isFull()) {
throw new IllegalStateException("Buffer is full");
}
buf"See full answer
"Approach 1: Use sorting and return the kth largest element from the sorted list. Time complexity: O(nlogn)
Approach 2: Use max heap and then select the kth largest element. time complexity: O(n+logn)
Approach 3: Quickselect. Time complexity O(n)
I explained my interviewer the 3 approaches. He told me to solve in a naive manner. Used Approach 1 had some time left so coded approach 3 also
The average time complexity of Quickselect is O(n), making it very efficient for its purpose. However, in"
GalacticInterviewer - "Approach 1: Use sorting and return the kth largest element from the sorted list. Time complexity: O(nlogn)
Approach 2: Use max heap and then select the kth largest element. time complexity: O(n+logn)
Approach 3: Quickselect. Time complexity O(n)
I explained my interviewer the 3 approaches. He told me to solve in a naive manner. Used Approach 1 had some time left so coded approach 3 also
The average time complexity of Quickselect is O(n), making it very efficient for its purpose. However, in"See full answer
"def changeString(org: str,target:str) -> bool:
lOrg = len(org)
lTarget = len(target)
\# They have to be equal in lenght
if lOrg != lTarget:
return False
counter1 = Counter(org)
counter2 = Counter(target)
\# Counter internally iterates through the input sequence, counts the number of times a given object occurs, and stores objects as keys and the counts as values.
if counter1 != counter2:
return False
diff = sum(org[i] != target[i] for i in range(n))
return diff == 2 or (diff == 0 and any(v > 1 f"
Rafał P. - "def changeString(org: str,target:str) -> bool:
lOrg = len(org)
lTarget = len(target)
\# They have to be equal in lenght
if lOrg != lTarget:
return False
counter1 = Counter(org)
counter2 = Counter(target)
\# Counter internally iterates through the input sequence, counts the number of times a given object occurs, and stores objects as keys and the counts as values.
if counter1 != counter2:
return False
diff = sum(org[i] != target[i] for i in range(n))
return diff == 2 or (diff == 0 and any(v > 1 f"See full answer
"this assumes that the dependency among courses is in a growing order:
0 -> 1 -> 2 -> ...
if not, then the code will not work"
Gabriele G. - "this assumes that the dependency among courses is in a growing order:
0 -> 1 -> 2 -> ...
if not, then the code will not work"See full answer