Given two strings `ransomNote`

and `magazine`

, return `true`

if `ransomNote`

can be constructed from `magazine`

and `false`

otherwise.

Each letter in `magazine`

can only be used once in `ransomNote`

.

**Example 1:**

```
Input: ransomNote = "a", magazine = "b"
Output: false
```

**Example 2:**

```
Input: ransomNote = "aa", magazine = "ab"
Output: false
```

**Example 3:**

```
Input: ransomNote = "aa", magazine = "aab"
Output: true
```

**Constraints:**

`1 <= ransomNote.length, magazine.length <= $10^5$`

`ransomNote`

and`magazine`

consist of lowercase English letters.

If ransom note is bigger than magazine, then its not possible to create a substring. So, return false.

I am looking for a way where I can use the occurrence count of each letter of ransom note and match it with magazine's each letter occurrence count.

Suppose I take the letter "a" from ransom note and store the occurrences of "a" in a map, and while I traverse through magazine I will also store the occurrence of "a" in another map.

If count of magazine's "a" is greater than or equal to ransom's "a" count then we can create the word, else return false.

```
/**
* @param {string} ransomNote
* @param {string} magazine
* @return {boolean}
*/
var canConstruct = function(ransomNote, magazine) {
if(ransomNote.length === 0 || magazine.length === 0) {
return false;
}
if(ransomNote.length > magazine.length) {
return false;
}
// { key : character , value: countOfOccurrence};
let hasMap = {};
// 1st: Create the Map with magazine's character occurrence and count.
for(let m = 0; m < magazine.length; m++) {
const magChar = magazine[m];
if( hasMap[magChar] !== undefined ) {
hasMap[magChar] += 1;
} else {
hasMap[magChar] = 1;
}
}
/*
Now your map has been created
So, now you have to:
1. find values of ransom note's characters in the map
2. Reduce the count of the character(key)'s value by 1
3. Check if the count of the character is 0 or less than 0
4. If its less than 0, clearly ransom note can't be made.
*/
let isPossible = true;
for(let r = 0; r < ransomNote.length; r++ ) {
const ranChar = ransomNote[r];
if(hasMap[ranChar]) {
hasMap[ranChar] -= 1 ;
if(hasMap[ranChar] < 0) {
isPossible = false;
break;
}
} else {
isPossible = false;
break;
}
}
return isPossible;
};
```

I hope you got a new perspective of solving this problem easy in O(n) time complexity.

If you are JavaScript Developer and want to get into top tech company, follow me on Twitter.

👇🏻👇🏻

I tweet and write about Data Structures and Algorithms, System Design and Large Scale Systems.

]]>Write an efficient algorithm that searches for a value in an `m x n`

matrix. This matrix has the following properties:

- Integers in each row are sorted from left to right.
- The first integer of each row is greater than the last integer of the previous row.

This is the first thing that comes to mind.

Traverse the matrix's each row and column value by value and get the number.

**Time Complexity: O$(n^2)$**

Why do I need to optimise, because the problem clearly says, write an efficient algorithm.

So, let's try to write an efficient algorithm.

What I can figure out is there's no way of traversing the array other than an outer for loop, that will traverse each row.

But what I can think of is using the first property of the matrix.

Integers in each row are sorted from left to right.

Since the array is sorted, I can use Binary Search to search for the value, and the complexity of this search binary search is O(logn)

**As of now the complexity of this solution stands at $O(n) + O(logn) = O(nlogn)$**

If I am thinking correctly, I can leverage the second point of the problem statement as well i.e.,

The first integer of each row is greater than the last integer of the previous row.

So, the idea is same, if the searched number is smaller than the current element than it's definitely in the previous collection.

So, I have to do binary search on columns as well, and complexity again becomes

$**O(logn)**$

So, adding row search with column search, we get

O$(logn)$ + O$(logn)$ = O$(logn)$

```
/**
* @param {number[][]} matrix
* @param {number} target
* @return {boolean}
*/
const binarySearchOnColumn = (matrix , target) => {
if(matrix.length === 1) {
return matrix;
}
const columnSize = matrix.length;
let midOfColumn = Math.floor(columnSize/2);
let slicedArray;
if(matrix[midOfColumn][0] > target ) {
slicedArray = matrix.slice(0 , midOfColumn);
} else {
slicedArray = matrix.slice(midOfColumn, matrix.length)
}
const finalMatrix = binarySearchOnColumn(slicedArray, target);
return finalMatrix;
}
const binarySearchOnRow = (workingArray,target)=>{
if (workingArray.length === 1) {
return workingArray[0] === target;
}
const rowSize = workingArray.length;
let midOfRow = Math.floor(rowSize / 2);
let sliceArray;
if (workingArray[midOfRow] > target) {
slicedArray = workingArray.slice(0, midOfRow);
} else {
slicedArray = workingArray.slice(midOfRow, workingArray.length)
}
return binarySearchOnRow(slicedArray, target);
}
var searchMatrix = function(matrix, target) {
const workingRow = binarySearchOnColumn(matrix , target);
const isElementFound = binarySearchOnRow(workingRow[0], target);
return isElementFound;
};
```

Results

If you find my thought process helpful, do tell me in the comments and don't forget to follow me on Twitter @sagars_01

]]>I have been interviewing a lot of folks in recent years, and I should tell you that the listed interview questions are not really frontend technology specific. These are for software engineers who are going to work in some cool products which serves customers at scale.

So, here are my favourite picks in no specific order with which I measure a candidate and also face a quite lot of time in my interviews.

Explain me "this" without using any JavaScript programming language reference, use a real world example.

Explain me event loop with a tiniest example.

Stock buy and sell problem : https://www.geeksforgeeks.org/stock-buy-sell/

Find duplicates in a string, optimise if you are traversing linearly.

Sorting domain names : https://stackoverflow.com/questions/342590/sorting-domain-names

Numeric keypad problem : https://www.geeksforgeeks.org/mobile-numeric-keypad-problem/

Coin change problem based on dynamic programming : https://www.geeksforgeeks.org/coin-change-dp-7/

Write tests of the previous question.

Design a schema for an order page in an e-commerce site.

Let's discuss about optimising a server rendered react application.

There are thousand of other great questions out there to prepare for an interview, but these are bare minimum that you can expect in a JavaScript interview. I am working for Lowes' where we are serving more than ~120M active users to buy and discover more than a million products. Working in these high performing tech companies, requires you to prepare holistically as a software engineer. Next post will be on system designs round.

If you want to know more on tech, product, interviews and JavaScript, follow me on twitter https://twitter.com/sagars_01

See you there.

]]>