Member-only story

JavaScript Interview Prep: Sparse Arrays

Prompt

Marika Lam
2 min readJun 22, 2024

There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results.

Solution

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});

process.stdin.on('end', function() {
inputString = inputString.split('\n');

main();
});

function readLine() {
return inputString[currentLine++];
}

/*
* Complete the 'matchingStrings' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts following parameters:
* 1. STRING_ARRAY strings
* 2. STRING_ARRAY queries
*/

function matchingStrings(strings, queries) {
// Write your code here'

const resArr = new Array(queries.length);
console.log(resArr);
queries.forEach((each, eachIndex) => {
let count = 0;
strings.forEach((each1, each1Index) => {
if (each == each1){
count++;
}
});
resArr[eachIndex] = count;
});

console.log(resArr);
return resArr;

}

function main() {
const ws =…

--

--

No responses yet