Skip to main content

Mid AlgoExp - 25-35

  • 25 Breadth First Search
  • 26 Youngest Common Ancestor
  • 27 Remove Islands
  • 28 Cycle In Graph
  • 29 Minimum Passes of Matrix
  • 30 Task Assignment
  • 31 Valid Starting City
  • 32 Min Heap Construction
  • 33 Linked List Construction
  • 34 Remove Kth Node From End
  • 35 Sum of Linked Lists

You're given a Node class that has a name and an array of optional children nodex. When put together, nodes form an acyclic tree-like structure

Implement the breathFirstSearch method on the Node class, which takes in an empty array, traverses the tree using the Breadth-first Search approach (specifically navigating the tree form left to right), stores all of the nodes' names in the input array, and returns it.

Starter code
class Node {
constructor(name) {
this.name = name;
this.children = [];
}

addChild(name) {
this.children.push(new Node(name));
return this;
}

breadthFirstSearch(array) {
// Write your code here.
}
}
👨‍🔬 Solutions
function status() {
console.log("On Development - Coming Soon!");
}
def status():
print("On Development - Coming Soon using Python!")

class HelloWorld {
public static void main(String[] args) {
System.out.println("On Development - Coming Soon using Java!");
}
}

#include <iostream>

int main() {
std::cout << "On Development - Coming Soon using C++!";
return 0;
}

26 Youngest Common Ancestor

Starter code
// This is an input class. Do not edit.
class AncestralTree {
constructor(name) {
this.name = name;
this.ancestor = null;
}
}

function getYoungestCommonAncestor(topAncestor, descendantOne, descendantTwo) {
// Write your code here.
}

// Do not edit the lines below.
exports.AncestralTree = AncestralTree;
exports.getYoungestCommonAncestor = getYoungestCommonAncestor;

👨‍🔬 Solutions
function status() {
console.log("On Development - Coming Soon!");
}
def status():
print("On Development - Coming Soon using Python!")

class HelloWorld {
public static void main(String[] args) {
System.out.println("On Development - Coming Soon using Java!");
}
}

#include <iostream>

int main() {
std::cout << "On Development - Coming Soon using C++!";
return 0;
}

27 Remove Islands

RemoveIsland

Starter code
function removeIslands(matrix) {
// Write your code here.
return [];
}
👨‍🔬 Solutions
function removeIslands(matrix) {
for(let row = 0; row<matrix.length; row++){
for(let col = 0; col<matrix[row].length; col++){
rowIsBorder = row === 0 || row === matrix.length-1
colIsBorder = col === 0 || col === matrix[row].length-1
let isBorder = colIsBorder || rowIsBorder

if(!isBorder) continue
convertConnectedToTwos(row, col, matrix)
}

}

// 2->1 and 1->0

for(let row=0; row<matrix.length; row++){
for(let col =0; col<matrix[row].length; col++){
const color = matrix[row][col]
if(color==1) matrix[row][col] = 0
if(color==2) matrix[row][col] = 1

}
}

return matrix
}


function convertConnectedToTwos(currentRow, currentCol, matrix){
console.log("changing for...", currentRow, currentCol)
const stack = [[currentRow, currentCol]]

while(stack.length > 0){
const [row, col] = stack.pop()


const color = matrix[row][col]

if(color != 1) continue
matrix[row][col] = 2

const neighbors = getNeighbors(row, col, matrix)
for (neighbor of neighbors){
stack.push(neighbor)
}

}


}

// Returns Neighbors as long as they are in.
function getNeighbors(row, col, matrix){

const neighbors = []
if(row - 1 >= 0) neighbors.push([row-1, col])
if(row + 1 < matrix.length) neighbors.push([row+1, col])
if(col - 1 >= 0 ) neighbors.push([row, col-1])
if(col + 1 < matrix.length) neighbors.push([row, col+1])

return neighbors
}



// Do not edit the line below.
exports.removeIslands = removeIslands;
def status():
print("On Development - Coming Soon using Python!")

class HelloWorld {
public static void main(String[] args) {
System.out.println("On Development - Coming Soon using Java!");
}
}

#include <iostream>

int main() {
std::cout << "On Development - Coming Soon using C++!";
return 0;
}

28 Cycle In Graph

Starter code
function numberOfWaysToTraverseGraph(width, height) {
return 0;
}
👨‍🔬 Solutions
function status() {
console.log("On Development - Coming Soon!");
}
def status():
print("On Development - Coming Soon using Python!")

class HelloWorld {
public static void main(String[] args) {
System.out.println("On Development - Coming Soon using Java!");
}
}

#include <iostream>

int main() {
std::cout << "On Development - Coming Soon using C++!";
return 0;
}

29 Minimum Passes of Matrix

Starter code
function numberOfWaysToTraverseGraph(width, height) {
return 0;
}
👨‍🔬 Solutions
function status() {
console.log("On Development - Coming Soon!");
}
def status():
print("On Development - Coming Soon using Python!")

class HelloWorld {
public static void main(String[] args) {
System.out.println("On Development - Coming Soon using Java!");
}
}

#include <iostream>

int main() {
std::cout << "On Development - Coming Soon using C++!";
return 0;
}

30 Task Assignment

Starter code
function numberOfWaysToTraverseGraph(width, height) {
return 0;
}
👨‍🔬 Solutions
function status() {
console.log("On Development - Coming Soon!");
}
def status():
print("On Development - Coming Soon using Python!")

class HelloWorld {
public static void main(String[] args) {
System.out.println("On Development - Coming Soon using Java!");
}
}

#include <iostream>

int main() {
std::cout << "On Development - Coming Soon using C++!";
return 0;
}

31 Valid Starting City

Starter code
function numberOfWaysToTraverseGraph(width, height) {
return 0;
}
👨‍🔬 Solutions
function status() {
console.log("On Development - Coming Soon!");
}
def status():
print("On Development - Coming Soon using Python!")

class HelloWorld {
public static void main(String[] args) {
System.out.println("On Development - Coming Soon using Java!");
}
}

#include <iostream>

int main() {
std::cout << "On Development - Coming Soon using C++!";
return 0;
}

32 Min Heap Construction

Starter code
function numberOfWaysToTraverseGraph(width, height) {
return 0;
}
👨‍🔬 Solutions
function status() {
console.log("On Development - Coming Soon!");
}
def status():
print("On Development - Coming Soon using Python!")

class HelloWorld {
public static void main(String[] args) {
System.out.println("On Development - Coming Soon using Java!");
}
}

#include <iostream>

int main() {
std::cout << "On Development - Coming Soon using C++!";
return 0;
}

33 Linked List Construction

Starter code
function numberOfWaysToTraverseGraph(width, height) {
return 0;
}
👨‍🔬 Solutions
function status() {
console.log("On Development - Coming Soon!");
}
def status():
print("On Development - Coming Soon using Python!")

class HelloWorld {
public static void main(String[] args) {
System.out.println("On Development - Coming Soon using Java!");
}
}

#include <iostream>

int main() {
std::cout << "On Development - Coming Soon using C++!";
return 0;
}

34 Remove Kth Node From End

Starter code
function numberOfWaysToTraverseGraph(width, height) {
return 0;
}
👨‍🔬 Solutions
function status() {
console.log("On Development - Coming Soon!");
}
def status():
print("On Development - Coming Soon using Python!")

class HelloWorld {
public static void main(String[] args) {
System.out.println("On Development - Coming Soon using Java!");
}
}

#include <iostream>

int main() {
std::cout << "On Development - Coming Soon using C++!";
return 0;
}

35 Sum of Linked Lists

Starter code
function numberOfWaysToTraverseGraph(width, height) {
return 0;
}
👨‍🔬 Solutions
function status() {
console.log("On Development - Coming Soon!");
}
def status():
print("On Development - Coming Soon using Python!")

class HelloWorld {
public static void main(String[] args) {
System.out.println("On Development - Coming Soon using Java!");
}
}

#include <iostream>

int main() {
std::cout << "On Development - Coming Soon using C++!";
return 0;
}