"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
"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
"Maybe we can use this solution:
1, connect all the strings together, and add an integer value ahead each string.
2, use Huffmans algorithm to encode the step 1 result, to make the result size smaller.
3, return the root of Huffmans tree.
This solution man be slower than the common serialize method, but it can save a lot of memory, I think, at lease doing serialize is mainly for tranfering data or storing data."
Jordan Z. - "Maybe we can use this solution:
1, connect all the strings together, and add an integer value ahead each string.
2, use Huffmans algorithm to encode the step 1 result, to make the result size smaller.
3, return the root of Huffmans tree.
This solution man be slower than the common serialize method, but it can save a lot of memory, I think, at lease doing serialize is mainly for tranfering data or storing data."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
"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
"Java
import java.util.*;
public class Solution {
public static int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] result = new int[n];
// Step 1: Calculate left products
result[0] = 1;
for (int i = 1; i = 0; i--) {
resul"
Vignesh g 2. - "Java
import java.util.*;
public class Solution {
public static int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] result = new int[n];
// Step 1: Calculate left products
result[0] = 1;
for (int i = 1; i = 0; i--) {
resul"See full answer
"
from typing import List
def getnumberof_islands(binaryMatrix: List[List[int]]) -> int:
if not binaryMatrix: return 0
rows = len(binaryMatrix)
cols = len(binaryMatrix[0])
islands = 0
for r in range(rows):
for c in range(cols):
if binaryMatrixr == 1:
islands += 1
dfs(binaryMatrix, r, c)
return islands
def dfs(grid, r, c):
if (
r = len(grid)
"
Rick E. - "
from typing import List
def getnumberof_islands(binaryMatrix: List[List[int]]) -> int:
if not binaryMatrix: return 0
rows = len(binaryMatrix)
cols = len(binaryMatrix[0])
islands = 0
for r in range(rows):
for c in range(cols):
if binaryMatrixr == 1:
islands += 1
dfs(binaryMatrix, r, c)
return islands
def dfs(grid, r, c):
if (
r = len(grid)
"See full answer
"function findPrimes(n) {
if (n < 2) return [];
const primes = [];
for (let i=2; i <= n; i++) {
const half = Math.floor(i/2);
let isPrime = true;
for (let prime of primes) {
if (i % prime === 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primes.push(i);
}
}
return primes;
}
`"
Tiago R. - "function findPrimes(n) {
if (n < 2) return [];
const primes = [];
for (let i=2; i <= n; i++) {
const half = Math.floor(i/2);
let isPrime = true;
for (let prime of primes) {
if (i % prime === 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primes.push(i);
}
}
return primes;
}
`"See full answer
"from typing import List
def three_sum(nums: List[int]) -> List[List[int]]:
nums.sort()
triplets = set()
for i in range(len(nums) - 2):
firstNum = nums[i]
l = i + 1
r = len(nums) - 1
while l 0:
r -= 1
elif potentialSum < 0:
l += 1
"
Anonymous Roadrunner - "from typing import List
def three_sum(nums: List[int]) -> List[List[int]]:
nums.sort()
triplets = set()
for i in range(len(nums) - 2):
firstNum = nums[i]
l = i + 1
r = len(nums) - 1
while l 0:
r -= 1
elif potentialSum < 0:
l += 1
"See full answer
"Let’s say the matrix is m x n (i.e., m rows and n columns).
Start from the top-right corner of the matrix.
Move left if you see a 1.
Move down if you see a 0.
Keep track of the row index where you last saw the leftmost 1 — that row has the most 1s.
public class MaxOnesRow {
public static int rowWithMostOnes(int matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int maxRowIndex = -1;
int j = cols - 1; /"
Khushbu R. - "Let’s say the matrix is m x n (i.e., m rows and n columns).
Start from the top-right corner of the matrix.
Move left if you see a 1.
Move down if you see a 0.
Keep track of the row index where you last saw the leftmost 1 — that row has the most 1s.
public class MaxOnesRow {
public static int rowWithMostOnes(int matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int maxRowIndex = -1;
int j = cols - 1; /"See full answer