DEV Community

Cover image for CodeToday: "Find Intersection" Algorithm, Coderbyte

CodeToday: "Find Intersection" Algorithm, Coderbyte

Kurt Bauer
Both a Web & Mobile Developer with start-up experience, from front-end UI to back-end RESTful API design, my ultimate goal is to secure data privacy.
・4 min read


The Gist

I am constantly dealing with code, whether it be small algorithm exercises, creating projects for fun or work, or testing out new tech. Yet, I've realized that things start falling through the cracks once my Firefox browser hits around 55 open tabs. I do my best before pulling the emergency lever and save links to research later, but I know that with all the tools available I could be doing a better job of both helping others that may encounter the same problem, and documenting my small wins for future challenges.

Not everything has to be worthy of 1k or more likes, and not everything has to be constructed to perfectly impress prospective employers or colleagues. Sometimes you just nerd out about code and you want to store that somewhere to reflect on down the road, maybe perfect, maybe not.

Either way I think I've been too worried about someone being elitist or snobby about my learning, and I don't want that holding me back anymore. Maybe I come back and refactor my problems, maybe I don't.

As always, I'm all for constructive comments or learning if you'd approach the problem differently.

And that's okay! So after all of that intro into what I hope this series of constant posts will be, here's some of today's code!

The Problem Statement

Below is the problem I found on Coderbyte

Have the function FindIntersection(strArr) read the array of strings stored in strArr which will contain 2 elements: the first element will represent a list of comma-separated numbers sorted in ascending order, the second element will represent a second list of comma-separated numbers (also sorted). Your goal is to return a comma-separated string containing the numbers that occur in elements of strArr in sorted order. If there is no intersection, return the string false.

For example: if strArr contains ["1, 3, 4, 7, 13", "1, 2, 4, 13, 15"] the output should return "1,4,13" because those numbers appear in both strings. The array given will not be empty, and each string inside the array will be of numbers sorted in ascending order and may contain negative numbers.

The Whiteboard

Below are my initial impressions and notes, the brainstorming session which is much like the knockoff rumba currently banging itself into the walls of my living room.


Your brain when solving algorithms
  • my strArr consists of two elements, which are each strings. So a simple map won't be so simple, so prior assembly required
  • '(also sorted)' appears to mean that I won't have to worry about that
  • return, a comma separated string, so I'll have to make sure not to keep my data in an array after working with it
  • if none, return false, means I need to check with an if/else somewhere

The Solution

function FindIntersection(){
//1) Grab the first and second elements to be compared
  let firstString = strArr[0]
  let secondString = strArr[1]

//2) Create empty arrays to store elements, after converted from strings to numbers
  let firstElementArray = []
  let secondElementArray = []

//3) split() a string into an array of substrings
//4) map() calls the provided function once for each element in an array,
//in order, to iterate over each string element you want to covert to a
//number data type, and push to your array
//5) wrap each string element with Number(), to transform from string 
//data type to number data type
  firstString.split(',').map((oneNumber) => {
//6) build the same function for the next element in the array of strings
  secondString.split(',').map((oneNumber) => {

//7) create a variable to store list of numbers, called myAnswer
//8) use filter(), which creates an array filled with all array elements that pass a test
//9) create a test inside the filter function which uses includes(),
//which determines whether an array contains a specified element.
//Basically, is my secondElementArray element(e) included in my
//firstElementArray element(e) when compared?
//10) Wrap your returned answer inside a toString() method, 
//which returns a string with all the array values, separated by commas

let myAnswer = (secondElementArray.filter(

e => firstElementArray.includes(e))


//11) Check to find if numbers are there, if not, return false
    return false
  } else  {
    return myAnswer; 

The Retrospective

No one is invincible, and no one is just born with a talent for coding. This is about always having a growth mindset.

Here are some things that tripped me up and forced me to rethink my approach.

  • Tried to just force map over my initial array, didn't realized that I had 2 strings to deal with
  • I lost time trying to convert both strings in the first array, and then putting them into a single array. It was simpler for me to build to seperate functions. Though I believe I could just create a helper function and re-use to keep my code DRY.
  • I searched for my Number() method to convert my string elements
  • I initially had my returned array and searched for my toString() method
  • I actually forgot to include a check if there were no matches, and re-read the question when writing this blog post! To be fair I had past the CoderByte tests without it and didn't realize...

The Conclusion

I've written this out in a CodePen Snippet, since I personally am more of a hands on learner, so here's to hoping this helps you in some way too.

This is my first post in what I hope to be daily conversation and appreciation of coding that I find worth reflecting on.

Imposter Syndrome and Anxiety are real things that deeply affect performance. I hope this motivates someone out there to keep a growth mindset, don't be afraid to write about something even if you don't feel like you know enough about it. It doesn't matter if it's not pretty or if you feel like you're "not a real programmer" because your solution isn't the cleanest. Put positive energy out there, be vulnerable, and always try to be your own best friend! We can all do this.

Discussion (0)