"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
"Problem: Given an input string txt consisting of alphanumeric characters and the parentheses characters '(' & ')', write a function which removes the minimum number of characters to return a version of the string with properly balanced parenthesis.
Answer: You can do this with a counter.
Psuedo-Python
Start with counter = 0
output = []
Iterate through the string, every time you encounter a '(', increment the counter. Add the character to the output.
If you encounter a ')', decrement the coun"
Michael B. - "Problem: Given an input string txt consisting of alphanumeric characters and the parentheses characters '(' & ')', write a function which removes the minimum number of characters to return a version of the string with properly balanced parenthesis.
Answer: You can do this with a counter.
Psuedo-Python
Start with counter = 0
output = []
Iterate through the string, every time you encounter a '(', increment the counter. Add the character to the output.
If you encounter a ')', decrement the coun"See full answer
"Write a function which Caesar ciphers all the strings so that the first character is "a". Use ascii code points and the modulo operator to do this.
Use this function to create a hashmap between each string and the CC-a string. Then go through each key:value pair in the hashmap, and use the CC-a ciphered value as the key in a new defaultdict(list), adding the original string to the value field in the output."
Michael B. - "Write a function which Caesar ciphers all the strings so that the first character is "a". Use ascii code points and the modulo operator to do this.
Use this function to create a hashmap between each string and the CC-a string. Then go through each key:value pair in the hashmap, and use the CC-a ciphered value as the key in a new defaultdict(list), adding the original string to the value field in the output."See full answer
"Initialize left pointer: Set a left pointer left to 0.
Iterate through the array: Iterate through the array from left to right.
If the current element is not 0, swap it with the element at the left pointer and increment left.
Time complexity: O(n). The loop iterates through the entire array once, making it linear time.
Space complexity: O(1). The algorithm operates in-place, modifying the input array directly without using additional data structures.
"
Avon T. - "Initialize left pointer: Set a left pointer left to 0.
Iterate through the array: Iterate through the array from left to right.
If the current element is not 0, swap it with the element at the left pointer and increment left.
Time complexity: O(n). The loop iterates through the entire array once, making it linear time.
Space complexity: O(1). The algorithm operates in-place, modifying the input array directly without using additional data structures.
"See full answer
Software Engineer
Data Structures & Algorithms
+4 more
🧠Want an expert answer to a question? Saving questions lets us know what content to make next.
"Without using a recursive approach, one can perform a post-order traversal by removing the parent nodes from the stack only if children were visited:
def diameterOfTree(root):
if root is None:
return 0
diameter = 0
stack = deque([[root, False]]) # (node, visited)
node_heights = {}
while stack:
curr_node, visited = stack[-1]
if visited:
heightleft = nodeheights.get(curr_node.left, 0)
heightright = nodehe"
Gabriele G. - "Without using a recursive approach, one can perform a post-order traversal by removing the parent nodes from the stack only if children were visited:
def diameterOfTree(root):
if root is None:
return 0
diameter = 0
stack = deque([[root, False]]) # (node, visited)
node_heights = {}
while stack:
curr_node, visited = stack[-1]
if visited:
heightleft = nodeheights.get(curr_node.left, 0)
heightright = nodehe"See full answer
"There is a faster approach that solves the problem in O(n) time:
def find_duplicates(arr1, arr2):
arr1 = set(arr1)
res = []
for num in arr2:
if num in arr1:
res.append(num)
return res
`"
Victor H. - "There is a faster approach that solves the problem in O(n) time:
def find_duplicates(arr1, arr2):
arr1 = set(arr1)
res = []
for num in arr2:
if num in arr1:
res.append(num)
return res
`"See full answer
"function serialize(list) {
for (let i=0; i 0xFFFF) {
throw new Exception(String ${list[i]} is too long!);
}
const prefix = String.fromCharCode(length);
list[i] = ${prefix}${list[i]};
console.log(list[i])
}
return list.join('');
}
function deserialize(s) {
let i=0;
const length = s.length;
const output = [];
while (i < length) {
"
Tiago R. - "function serialize(list) {
for (let i=0; i 0xFFFF) {
throw new Exception(String ${list[i]} is too long!);
}
const prefix = String.fromCharCode(length);
list[i] = ${prefix}${list[i]};
console.log(list[i])
}
return list.join('');
}
function deserialize(s) {
let i=0;
const length = s.length;
const output = [];
while (i < length) {
"See full answer
"You are given a string S and a number K as input, and your task is to print S to console output considering that, at most, you can print K characters per line.
Example:
S = "abracadabra sample"
K = 11
Output:
abracadabra
sample
Note that this problem requires the interviewee gather extra requirements from the interviewer (e.g. do we care about multiple white spaces? what if the length of a word is greater than K, ...)"
B. T. - "You are given a string S and a number K as input, and your task is to print S to console output considering that, at most, you can print K characters per line.
Example:
S = "abracadabra sample"
K = 11
Output:
abracadabra
sample
Note that this problem requires the interviewee gather extra requirements from the interviewer (e.g. do we care about multiple white spaces? what if the length of a word is greater than K, ...)"See full answer
"\# Definition for a binary tree node.
class TreeNode:
def init(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def maxPathSum(self, root: TreeNode) -> int:
self.max_sum = float('-inf')"
Jerry O. - "\# Definition for a binary tree node.
class TreeNode:
def init(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def maxPathSum(self, root: TreeNode) -> int:
self.max_sum = float('-inf')"See full answer
"Count items between indices within compartments
compartments are delineated by by: '|'
items are identified by: '*'
input_inventory = "*||||"
inputstartidxs = [1, 4, 6]
inputendidxs = [9, 5, 8]
expected_output = [3, 0, 1]
Explanation:
"*||||"
0123456789... indices
++ + # within compartments
^ start_idx = 1
^ end_idx = 9
-- - # within idxs but not within compartments
"*||||"
0123456789... indices
"
Anonymous Unicorn - "Count items between indices within compartments
compartments are delineated by by: '|'
items are identified by: '*'
input_inventory = "*||||"
inputstartidxs = [1, 4, 6]
inputendidxs = [9, 5, 8]
expected_output = [3, 0, 1]
Explanation:
"*||||"
0123456789... indices
++ + # within compartments
^ start_idx = 1
^ end_idx = 9
-- - # within idxs but not within compartments
"*||||"
0123456789... indices
"See full answer
"def friend_distance(friends, userA, userB):
step = 0
total_neighs = set()
llen = len(total_neighs)
total_neighs.add(userB)
while len(total_neighs)!=llen:
s = set()
step += 1
llen = len(total_neighs)
for el in total_neighs:
nes = neighbours(friends, userA, el)
if userA in nes:
return step
for p in nes:
s.add(p)
for el in s:
total_neighs.add(el)
return -1
def neighbours(A,n1, n2):
out = set()
for i in range(len(A[n2])):
if An2:
out.add(i)
return out"
Batman X. - "def friend_distance(friends, userA, userB):
step = 0
total_neighs = set()
llen = len(total_neighs)
total_neighs.add(userB)
while len(total_neighs)!=llen:
s = set()
step += 1
llen = len(total_neighs)
for el in total_neighs:
nes = neighbours(friends, userA, el)
if userA in nes:
return step
for p in nes:
s.add(p)
for el in s:
total_neighs.add(el)
return -1
def neighbours(A,n1, n2):
out = set()
for i in range(len(A[n2])):
if An2:
out.add(i)
return out"See full answer
"This is just a two line solve, I'm not sure why it's being made more complicated?
function productExceptSelf(nums) {
// your code goes here
const product = nums.reduce((acc, val) => acc * val, 1);
return nums.map(num => num === 0 ? 0 : Math.floor(product/num));
}
`"
Marc S. - "This is just a two line solve, I'm not sure why it's being made more complicated?
function productExceptSelf(nums) {
// your code goes here
const product = nums.reduce((acc, val) => acc * val, 1);
return nums.map(num => num === 0 ? 0 : Math.floor(product/num));
}
`"See full answer
"def find_first(array: List[int], num: int) -> int:
lo = 0
hi = len(array)-1
while lo = num:
hi = mid - 1
if lo == mid and array[mid] == num:
return mid
else:
array[mid] < num
lo = mid + 1
return -1
`"
Gabriele G. - "def find_first(array: List[int], num: int) -> int:
lo = 0
hi = len(array)-1
while lo = num:
hi = mid - 1
if lo == mid and array[mid] == num:
return mid
else:
array[mid] < num
lo = mid + 1
return -1
`"See full answer
"Questions to ask :
Are there negative values in the input array? Interview : YES
Will the product of two number fit into 32-bit Integer. If not, will it fit 64-bit Integer. If not, then is it safe to use Big Integer? Interview : let's worry only about 32 bit Integer
What should we return if the input array is null or size (size of input array) is less than 2? Return 0
From above Information:
General approach is as follows :
a) Track smallest 2 elements in the array -> p"
Rajendra D. - "Questions to ask :
Are there negative values in the input array? Interview : YES
Will the product of two number fit into 32-bit Integer. If not, will it fit 64-bit Integer. If not, then is it safe to use Big Integer? Interview : let's worry only about 32 bit Integer
What should we return if the input array is null or size (size of input array) is less than 2? Return 0
From above Information:
General approach is as follows :
a) Track smallest 2 elements in the array -> p"See full answer