"class Node:
def init(self, value):
self.value = value
self.children = []
def inorder_traversal(root):
if not root:
return []
result = []
n = len(root.children)
for i in range(n):
result.extend(inorder_traversal(root.children[i]))
if i == n // 2:
result.append(root.value)
if n == 0:
result.append(root.value)
return result
Example usage:
root = Node(1)
child1 = Node(2)
chil"
Teddy Y. - "class Node:
def init(self, value):
self.value = value
self.children = []
def inorder_traversal(root):
if not root:
return []
result = []
n = len(root.children)
for i in range(n):
result.extend(inorder_traversal(root.children[i]))
if i == n // 2:
result.append(root.value)
if n == 0:
result.append(root.value)
return result
Example usage:
root = Node(1)
child1 = Node(2)
chil"See full answer
"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
"MOD = 10**9 + 7
def max_stability(reliability, availability):
max_stability = 1
for r, a in zip(reliability, availability):
Compute stability of the current server
stability = r * a
if stability != 0:
Multiply into max_stability and take modulo
maxstability = (maxstability * stability) % MOD
return max_stability
reliability = [1, 2, 2]
availability = [1, 1, 3]
print(max_stability(reliability, availability)) # Output the result mo"
K.nithish K. - "MOD = 10**9 + 7
def max_stability(reliability, availability):
max_stability = 1
for r, a in zip(reliability, availability):
Compute stability of the current server
stability = r * a
if stability != 0:
Multiply into max_stability and take modulo
maxstability = (maxstability * stability) % MOD
return max_stability
reliability = [1, 2, 2]
availability = [1, 1, 3]
print(max_stability(reliability, availability)) # Output the result mo"See full answer
"def mostefficientseqscore(parentheses, efficiencyratings):
mes = []
for i in range(len(parentheses)):
mes.append((parentheses[i], max(efficiency_ratings[i]))
return sum([m[1] for m in mes])
`"
Nathan C. - "def mostefficientseqscore(parentheses, efficiencyratings):
mes = []
for i in range(len(parentheses)):
mes.append((parentheses[i], max(efficiency_ratings[i]))
return sum([m[1] for m in mes])
`"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
Software Engineer
Coding
+4 more
🧠 Want an expert answer to a question? Saving questions lets us know what content to make next.
"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
"#inplace reversal without inbuilt functions
def reverseString(s):
chars = list(s)
l, r = 0, len(s)-1
while l < r:
chars[l],chars[r] = chars[r],chars[l]
l += 1
r -= 1
reversed = "".join(chars)
return reversed
"
Anonymous Possum - "#inplace reversal without inbuilt functions
def reverseString(s):
chars = list(s)
l, r = 0, len(s)-1
while l < r:
chars[l],chars[r] = chars[r],chars[l]
l += 1
r -= 1
reversed = "".join(chars)
return reversed
"See full answer
"Abstract class
A class that can have Abstract methods - without implementations and Concerete Methods i.e with implementation.
Can have private, protected and public access modifiers.
Supports Single inheritance i.e a class can extend only 1 abstract class
Can have constructors
Mainly used when sharing common behaviors
Interface Class
A collection of abstract methods ( can have static and default methods also - onwards of java 8)
Public, static, final are the access"
Sue G. - "Abstract class
A class that can have Abstract methods - without implementations and Concerete Methods i.e with implementation.
Can have private, protected and public access modifiers.
Supports Single inheritance i.e a class can extend only 1 abstract class
Can have constructors
Mainly used when sharing common behaviors
Interface Class
A collection of abstract methods ( can have static and default methods also - onwards of java 8)
Public, static, final are the access"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
"python:
def justifywords(wordslist, width):
result = []
currlinechar_count = 0
curr_words = []
for word in words_list:
if curr_words:
space_needed = len(word) + 1 # Space needed for the word and a preceding space
else:
space_needed = len(word)
if currlinecharcount + spaceneeded > width:
result.append(' '.join(curr_words))
curr_words = [word]
currlinechar_count = len("
Anonymous Unicorn - "python:
def justifywords(wordslist, width):
result = []
currlinechar_count = 0
curr_words = []
for word in words_list:
if curr_words:
space_needed = len(word) + 1 # Space needed for the word and a preceding space
else:
space_needed = len(word)
if currlinecharcount + spaceneeded > width:
result.append(' '.join(curr_words))
curr_words = [word]
currlinechar_count = len("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 encode(root):
if not root:
return []
def dfs(node):
if not node:
return
res.append(node.val)
res.append(len(node,children))
for child_node in node.children:
dfs(child_node)
res = []
dfs(root)
return res
def decode(arr):
if not arr:
return None
n = len(arr)
i = 0
def dfs(val, children_count):
if children_count == 0:
return Node(val)
cur_node = Node(val)
cur_node.children = []
for j in range(children_count):
nonlocal i
i += 2
cur_node.children.append(dfs(arr[i], arr[i"
Ying T. - "def encode(root):
if not root:
return []
def dfs(node):
if not node:
return
res.append(node.val)
res.append(len(node,children))
for child_node in node.children:
dfs(child_node)
res = []
dfs(root)
return res
def decode(arr):
if not arr:
return None
n = len(arr)
i = 0
def dfs(val, children_count):
if children_count == 0:
return Node(val)
cur_node = Node(val)
cur_node.children = []
for j in range(children_count):
nonlocal i
i += 2
cur_node.children.append(dfs(arr[i], arr[i"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
"First of all, stack and heap memory are abstraction on top of the hardware by the compiler. The hardware is not aware of stack and heap memory. There is only a single piece of memory that a program has access to. The compiler creates the concepts of stack and heap memory to run the programs efficiently.
Programs use stack memory to store local variables and a few important register values such as frame pointer and return address for program counter. This makes it easier for the compiler to gene"
Stanley Y. - "First of all, stack and heap memory are abstraction on top of the hardware by the compiler. The hardware is not aware of stack and heap memory. There is only a single piece of memory that a program has access to. The compiler creates the concepts of stack and heap memory to run the programs efficiently.
Programs use stack memory to store local variables and a few important register values such as frame pointer and return address for program counter. This makes it easier for the compiler to gene"See full answer
"naive solution:
def countprefixpairs(words):
n = len(words)
count = 0
for i in range(n):
for j in range(i + 1, n):
if words[i].startswith(words[j]) or words[j].startswith(words[i]):
count += 1
return count
using tries for when the list of words is very long:
from collections import Counter
class TrieNode:
def init(self):
self.children = {}
self.count = 0 # To count the number of words ending at this node"
Anonymous Unicorn - "naive solution:
def countprefixpairs(words):
n = len(words)
count = 0
for i in range(n):
for j in range(i + 1, n):
if words[i].startswith(words[j]) or words[j].startswith(words[i]):
count += 1
return count
using tries for when the list of words is very long:
from collections import Counter
class TrieNode:
def init(self):
self.children = {}
self.count = 0 # To count the number of words ending at this node"See full answer