Author Archives: James

Implement a stack API using only a heap. A stack implements the following methods:

By | January 31, 2024

Recall that a heap has the following operations: To implement a stack using only a heap, you can use a max heap and maintain a counter to ensure that the order of elements is preserved. Here’s an example implementation in Python, Java, and JavaScript using the heapq module (Python), PriorityQueue (Java), and BinaryHeap (JavaScript): Python:… Read More »

Given a string of parentheses, find the balanced string that can be produced from it using the minimum number of insertions and deletions

By | January 25, 2024

Given a string of parentheses, find the balanced string that can be produced from it using the minimum number of insertions and deletions. If there are multiple solutions, return any of them. For example, given “(()”, you could return “(())”. Given “))()(“, you could return “()()()()”. To find the balanced string with the minimum number… Read More »

Write a program to merge two binary trees. Each node in the new tree should hold a value equal to the sum of the values of the corresponding nodes of the input trees. If only one input tree has a node in a given position, the corresponding node in the new tree should match that input node.

By | January 15, 2024

Asked by SalesForce JavaScript: Java: Python: These programs define a TreeNode class and a function (mergeTrees in JavaScript, mergeTrees method in Java, and merge_trees function in Python) to merge two binary trees following the specified rules. You can adapt these examples based on your specific requirements.

Given a string, return the length of the longest palindromic subsequence in the string- Asked by Google

By | December 21, 2023

Given a string, return the length of the longest palindromic subsequence in the string. For example, given the following string: MAPTPTMTPA Return 7, since the longest palindromic subsequence in the string is APTMTPA. Recall that a subsequence of a string does not have to be contiguous! Your algorithm should run in O(n^2) time and space.… Read More »

Implement a PrefixMapSum class with the following methods

By | July 5, 2023

insert(key: str, value: int): Set a given key’s value in the map. If the key already exists, overwrite the value. sum(prefix: str): Return the sum of all values of keys that begin with a given prefix. For example, you should be able to run the following code: mapsum.insert(“columnar”, 3) assert mapsum.sum(“col”) == 3 mapsum.insert(“column”, 2)… Read More »

Problem: You are given N identical eggs and access to a building with k floors. Your task is to find the lowest floor that will cause an egg to break, if dropped from that floor.

By | July 2, 2023

You are given N identical eggs and access to a building with k floors. Your task is to find the lowest floor that will cause an egg to break, if dropped from that floor. Once an egg breaks, it cannot be dropped again. If an egg breaks when dropped from the xth floor, you can… Read More »

Create an algorithm that arranges them in order to form the largest possible integer

By | July 2, 2023

Given a list of numbers, create an algorithm that arranges them in order to form the largest possible integer. For example, given [10, 7, 76, 415], you should return 77641510. Javascript function largestNumber(nums) {  nums.sort((a, b) => {    const order1 = String(a) + String(b);    const order2 = String(b) + String(a);    return order2.localeCompare(order1); … Read More »

Problem: There are N prisoners standing in a circle, waiting to be executed…

By | June 27, 2023

There are N prisoners standing in a circle, waiting to be executed. The executions are carried out starting with the kth person, and removing every successive kth person going clockwise until there is no one left. Given N and k, write an algorithm to determine where a prisoner should stand in order to be the… Read More »

Given a sorted array, find the smallest positive integer that is not the sum of a subset of the array.

By | June 27, 2023

Java public class SmallestPositiveInteger {    public static int findSmallestPositiveInteger(int[] nums) {        int smallest = 1;        for (int num : nums) {            if (num <= smallest) {                smallest += num;            } else… Read More »

Write a program to compute the in-order traversal of a binary tree using O(1) space.

By | June 25, 2023

Python Java Javascript These implementations use the Morris Traversal algorithm to perform in-order traversal without using any additional space other than O(1). The algorithm establishes temporary links between nodes and their predecessors, allowing efficient traversal and processing of the tree nodes. Note: The code examples assume that the tree nodes have a val, left, and… Read More »

Flutter vs. React Native: Unraveling the Performance Battle

By | June 25, 2023

Introduction: When it comes to developing cross-platform mobile applications, Flutter and React Native have emerged as the two dominant frameworks. As developers seek to create high-performing apps, it’s crucial to understand the performance capabilities of each framework. In this article, we will delve into the performance aspects of Flutter and React Native, examining their strengths… Read More »

Different ways of making rounded corner images in flutter

By | June 25, 2023

Example 1: ClipRRect dartCopy codeClipRRect( borderRadius: BorderRadius.circular(10.0), child: Image.asset(‘assets/images/image.jpg’), ) In this example, the ClipRRect widget is used to clip the child Image with rounded corners. The borderRadius property defines the radius of the corners. Example 2: BoxDecoration dartCopy codeContainer( decoration: BoxDecoration( borderRadius: BorderRadius.circular(10.0), image: DecorationImage( image: AssetImage(‘assets/images/image.jpg’), fit: BoxFit.cover, ), ), ) Here, a… Read More »

Given an absolute pathname that may have . or .. as part of it, return the shortest standardized path

By | June 25, 2023

Given an absolute pathname that may have . or .. as part of it, return the shortest standardized path. For example, given “/usr/bin/../bin/./scripts/../”, return “/usr/bin/”. To obtain the shortest standardized path from an absolute pathname that may contain “.” or “..” as part of it, you can follow these steps: Here’s an example Python code… Read More »

Riverpod in Flutter – Simplest Example Ever

By | June 22, 2023

In this example, we create a simple Flutter application that displays a counter and increments the count when a FloatingActionButton is pressed. The state management is handled by Riverpod. Here’s a breakdown of the code: When you run this code, you will see a simple app with an app bar, a counter in the center… Read More »

Connect 4 is a game where opponents take turns dropping red or black discs into a 7 x 6 vertically suspended grid.

By | June 22, 2023

The game ends either when one player creates a line of four consecutive discs of their color (horizontally, vertically, or diagonally), or when there are no more spots left in the grid. Design and implement Connect 4. To design and implement Connect 4, we can follow these steps: Here’s an example implementation of Connect 4… Read More »

Roman numeral format to decimal.

By | June 22, 2023

Given a number in Roman numeral format, convert it to decimal. The values of Roman numerals are as follows: { ‘M’: 1000, ‘D’: 500, ‘C’: 100, ‘L’: 50, ‘X’: 10, ‘V’: 5, ‘I’: 1 } In addition, note that the Roman numeral system uses subtractive notation for numbers such as IV and XL. For the… Read More »

Problem Solving – IP addresses must follow the format A.B.C.D, where A, B, C, and D are numbers between 0 and 255.

By | June 19, 2023

Given a string of digits, generate all possible valid IP address combinations. IP addresses must follow the format A.B.C.D, where A, B, C, and D are numbers between 0 and 255. Zero-prefixed numbers, such as 01 and 065, are not allowed, except for 0 itself. For example, given “2542540123”, you should return [‘254.25.40.123’, ‘254.254.0.123’]. To… Read More »

Custom Theme using Theme Extensions

By | December 16, 2022

Theme extensions were introduced in Flutter 3. But what are Theme Extensions? As the name says, it helps to extend the inbuilt themes with our own extensions. Let’s jump into an example So when you create a flutter app, your basic root widget will look like this return MaterialApp( title: ‘Flutter Theme Extensions’, theme: ThemeData(… Read More »