DEV Community

Cover image for How-To check a pattern into a string & retrieve it

Posted on

How-To check a pattern into a string & retrieve it

This is an interview test so, there are no unique questions, even correct or incorrect. Because it was meant to see how a developer think and develop itself with their knowledge.

This was my answer...

const string = 'qweaawq23,4ñmñleaaaaa23rt-.y90_4gsdn_klaaaaa23-4nper-etrç.mn4wetaaaaaaaaasx+';
const patt = /a+/g;

const result = string.match(patt)
// what catch the regex
const arrSizes = item => item.length )
let bigger = []
let longest = 0
result.forEach( (item, index) => {
  if( item.length > longest ){
    bigger = [index, item, item.length]
    longest = parseInt(item.length)
// what's the result of the loop
// checking correlation
Enter fullscreen mode Exit fullscreen mode

Why do this ?

Checking a pattern it's not easy, even less into a string because there are no marks or flags that tell us by them self when something start and end. So, get that coordinates is the developer task.

My original idea was to extract all the patterns encountered in the string, get them into an array and compare each other to find the longest, maybe with a kind of loop.

So, regex was my first choice: but why? Because Regex are very "smart" or very efficient looking for things, they are in many situations the best option to find certain kinds of patterns: like strings.

But, that was my firts and easiest option so I decide to try before with a for loop. Result: DISASTER

The for loop disaster: a horror experience based in a real history.

Trying a for loop was very very cubersome... Because that from before: there are no marks or flags that tell us by them self when something start and end. So trying to set this marks was veeeeeeeeeeeeeeeeeeeeery borring and not efficient from my perspective, even more, this reinforced the idea about use regex.

Some problems encountered was:

  1. build a stop mark for : pattern comparison
  2. build a stop mark for : length counter between candidates
  3. exponentiation of variables needed
  4. etc...

regex : a happy ending

The proper expression let me fit the requirements to a logic, like "recognize one or more coincidences of 'a' (/a+/) in the string (g)" : /a+/g

From that point the logic flows into more or less basic structure.

So, to apply .match method to the string gives me the array of patterns I dream at the first look.

Then, .map (that return a new array) provides the longest length but I thought that It was not useful to retrieve the longest pattern if we dont know where is it or we cant have it, so...

From my personal point of view to achieve this, an iteration over each of the items could be a good option, in this case where time or memory consumption it wasn't a must.

Finally, in the sake of keep all simple I use an array variable called bigger to save the bigger pattern and their index to have a reference of their position in the result array to be retrieved.

A numeric variable with the longest value, because this logic is inspired in a binary algoritmic search so, my desition was to compare the first item length with zero and whatever value it was this allow to continue the comparisions in a progresive manner.

Why is inspired in a binary search? Because comparisons increase in an exponential manner. This means that 4 items would give six posibilities and 10 items would give 45. So, in a binary search inspiration logic one value would be compared with one another value that result in one possibility, keeping things flat.

By last, a if statement check an absolute length that belongs to the longest pattern with the actual length. If true, an array was choosen to keep the pattern and their index.


Awesome super fun test, the result is a logic that return the index of the longest pattern, the pattern, and their length.

Top comments (0)