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) assert mapsum.sum(“col”) ==… Read More »

James

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 assume it will… Read More »

James

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);  });  return nums.join(”);}//… Read More »

James

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 right property to… Read More »

James

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 and weaknesses to… Read More »

James

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 Container is used… Read More »

James

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 that implements this… Read More »

James

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 of the screen,… Read More »

James

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 in Python: class… Read More »

James

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 input XIV, for… Read More »

James

Smallest sparse number greater than or equal to N.

By | June 20, 2023

We say a number is sparse if there are no adjacent ones in its binary representation. For example, 21 (10101) is sparse, but 22 (10110) is not. For a given input N, find the smallest sparse number greater than or equal to N. Do this in faster than O(N log N) time. Solution in Java and Javascript Java… Read More »

James

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 generate all possible… Read More »

James

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( primarySwatch: Colors.blue, ),… Read More »

James

Flutter Best Practices — Part 2

By | October 17, 2022

1. Avoid Functional Widgets We usually have a situation where we need to separate out UI code from the widget, But we avoid creating a separate widget and use function which returns Widget. This practice have some benefits, like you don’t need to pass all parameters in your new widget, You have less code and less files. But this approach… Read More »

James

Flutter Best Practices — Part 1

By | October 13, 2022

1. Placeholder Widgets Use SizedBox instead of Container as Placeholder widgets. Take a look at the below use-case return _loaded ? Container() : YourWidget(); The SizedBox is a const constructor and creates a fixed-size box. The width and height parameters can be null to indicate that the size of the box should not be constrained in the corresponding dimension. Hence, when we… Read More »

James

Debugging Network Calls in Flutter with Alice

By | June 21, 2022

In this article I am showing a useful plugin to debug network calls in Flutter. Alice! You can find the alice package here. Alice is an HTTP Inspector tool for Flutter which helps debugging http requests. It catches and stores http requests and responses, which can be viewed via simple UI. It is simple to integrate! First add… Read More »

James

Chocolate Feast Problem

By | June 10, 2021

Little Bobby loves chocolate. He frequently goes to his favorite 5 & 10 store, Penny Auntie, to buy them. They are having a promotion at Penny Auntie. If Bobby saves enough wrappers, he can turn them in for a free chocolate. Example n = 15 c = 3 m = 2 Answer 5 + 2 + 1 +… Read More »

James

Minimum Distances between Similar numbers in an array

By | June 8, 2021

The distance between two array values is the number of indices between them. Given , find the minimum distance between any pair of equal elements in the array. If no such value exists, return -1. Example arr = [3, 2, 1, 2, 3]; Minimum Distance = 2 (Distance between 2’s)

James

MVVM in Android – ViewModels, ViewModelScope, Retrofit all with a Simple Example.

By | June 6, 2021

Let’s learn how to implement MVVM in Android. It’s actually really simple. ViewModel is just another class that that implements the main Business Logic. ViewModel is a middle man between your view and your Data class(Repo/any Data Provider). It can be represented simply like this. Here the View just displays the data. ViewModel doesn’t know where the data… Read More »

James

Theming your App in Flutter using BLoc, Save & Reload

By | December 6, 2020

In this article, we will see how we can use BLoc to Theme your app in Flutter. To understand BLoC with a real world example, you can refer to my previous post on BLoC here. Part 1 Part 1 For this demo also we will need the below plugins If you follow my previous tutorial, it would be… Read More »

James