My first technical interview, I'm stuck and need help

Hi campers,

A quick background, here for five years, received FCC’s Javascript and Frontend Libraries certification. Friday got first technical interview which is:

Two algorithm problems (‘The perfect sum Pair’ and ‘Jewel Spelunker’ ) I can do it at home, won’t be timed, monitored and send it when Im done.

Spent whole day on it but can’t figure it out . I don’t want to cheat and copy the answer but also I want to pass this interview. Also the faster I finish it the better.

Only idea I have it to post it on FCC forum or stackoverflow with my solution so far, then people could give hints.

Any ideas or suggestions would be great :slight_smile:

Generally question: How long should I spend on solving an algorithm question before I ask for help? Usually spend 5-6 hours, even two days on one question.

Thanks

4 Likes

I follow the thread because I want to know too.
have you been studying front end dev for 5 years?? What is your level in javascript I am new too, so I want to know if I am good enough to start a job hunt

What is your level in javascript I am new too, so I want to know if I am good enough to start a job hunt

Yes 5 years but not full-time more like half effort. Having said that I’m comfortable with javascript but I wouldn’t say I’m very good. for example, I can make basic app in React.

I want to know if I am good enough to start a job hunt

honestly I don’t know the answer to that, but maybe someone here with more experience can.

thank you for the reply and the advice :slight_smile:

As to when to start the job hunt? I would say sooner than you think - you might get lucky and the experience of the hunt it valuable too.

As you your algorithms… Which problem? What is hanging you up? Otherwise you’re just asking us to write out the entire pseudocode for you.

2 Likes

“The Perfect Sum Pair” is the question:

Given an array of sorted integers, return the index (counting from zero) of two numbers such
that they add up to a specific target. You may assume that each case would have exactly one
solution, and you may not use the same element twice

Input: numbers = [2, 7, 11, 15], target = 9
Output: [0, 1]
Explanation: 2+7 equals 9 and their indexes are 0 and 1.

I have so far this instead of [0,1] I get [0,1,1,0]

const array = [2, 7, 11, 15] 
const addsUpTo = 9

const limit = array.length

let answer = []

function perfectSumPair (numbers, target) {

    for(let i = 0; i < limit; i++) {
        for(let j = limit - 1; j>= 0; j--) {
            if(numbers[i]+ numbers[j] === target && numbers.find((e) => e !== i && e !==j)) {
        
               answer.push(i,j) 
            }
            else{
            }
        }
    }
    return answer

}

console.log(perfectSumPair(array, addsUpTo))

First map out your algorithm. Make sure it works with the test cases before writing your code. Explain your algorithm to us first. We can tell you if you have it correct first. Since this is for a job, we will not create the algorithm or code for you. I don’t mind telling you if your algorithm is correct though.

1 Like

Thanks for your response

First map out your algorithm. Make sure it works with the test cases before writing your code. Explain your algorithm to us first.

Not sure what do you mean by "mapping out my algorithm ", like a pseudocode?

Ok I can explain the algorithm here

When I’m stuck I add a bunch of console.log statements (sometimes after almost every functional line) so I can see exactly what the program is doing. That said, I noticed that your code returns [0,1,1,0] for me, which is very different from [0,1,10]. I think if you consider what you want your function to do and “map out the algorithm” a bit you’ll see your error.

2 Likes

@Realitydistortionfie You could use regular language or pseudo-code if you like. The main thing is to be able to explain what steps your code (in any language) would need to take to solve the problem. The thing to realize about the first problem is that your algorithm should be very simple do to the fact that the array elements are integers and the array is already sorted.

I had never seen this specific problem before, but it is in a family of problems that I have seen before. It took me about 20-25 minutes to write out my algorithm and then about 5 minutes to write the actual JavaScript. It consists of about 10 lines of code using nothing but basic JavaScript (no higher order functions). I then condensed it down to 3 lines of code using a higher order function.

2 Likes

This.

When in doubt, print it out!

Even if you think you “know” what a section does, really spell out every single step:

“Entering the loop”
“Pushed to array”
“Leaving Loop”

etc etc

2 Likes

Like @jonathanrace77 says when in doubt print out!
As far as hints I can give you some.
You have to go through the whole array one by one until the end or until the condition is met .
At the same time you have to go through the rest of the array one index after the one you are currently in to check the rest of the numbers.
You have to take the idx you are in and the next index and sum them to check if it’s equal to the target. If it is there you have to return those two indexes.

I hope this helps and I hope you can solve it! Also, don’t burn yourself out. If you have been approaching this for too long take a break, go for a walk, or do something that will give you some ease. Sometimes we just need a break to code with more clarity!

2 Likes

Addressing the General Question:

  • You’d factor in who you have to help you?
  • Who you’re working with and how helpful they are/want to be?
  • When is the project due?
  • Where are you stuck specifically, meaning; is it a function? Is it brand new material?

I had the same question when I started but now I realize it depends on so much that it shouldn’t be a concern.

I’ve worked with gifted people who absorb knowledge just seeing it once. I’ve also worked with lazy people you gotta tell thing’s to them 3-4 times.
In the end, its the circumstances.

Anyone trying to give you a hard time Is either full of crap or gifted, in which case you shouldn’t compare anyway.

1 Like

Thanks everyone for the ideas.

This is a great community :slight_smile:

What do you think of this

1st for loop: selects first element in array
2nd forloop: selects other elements in array
compare the selected elements
do they add up to target?
if yes find index of those numbers (or use i & j from forloops )
make sure indexes not used before
push it to new array
return new array

I managed to solve it with a small modification to my original code. But I don’t understand why it works. :slight_smile:

I changed answer.push(i,j) to answer.push(i)
Even works with other cases
numbers = [-1, 3, 5] target = 4
returns [0, 2]

Full code here. Now correctly returns [0,1]

const array = [2, 7, 11, 15] 
const addsUpTo = 9
const limit = array.length
let answer = []

function perfectSumPair (numbers, target) {
    for(let i = 0; i < limit; i++) {
        for(let j = limit - 1; j>= 0; j--) {
            if(numbers[i]+ numbers[j] === target && numbers.find((e) => e !== i || e !==j)) {
        
               answer.push(i) 
            }
            else{
            }
        }
    }
    return answer
}

console.log(perfectSumPair(array, addsUpTo))

Confused because i represents the index of first element that adds up to target. But also I need the index of second element, represented by j. So why just pushing i answer.push(i) works?

Mine is not very simple and also I didn’t take advantage of the the fact that is sorted. Where can I learn more about this

My code is messy I can make it shorter. I think sending it as is will make me look bad. I used one higher order function and still 20 lines. I should try mapping my algorithm again or I need to take some time and read a about basics of algorithm and data structures?

This is a simple algorithm. I have implemented it in five minutes and i’m just two months in learning Python. The point was not to give you the solution, but to read the suggested implementation (in fact the nested loop approach is hinted as a “brute force” method while the second method should be a better solution in terms of efficiency). Cheers.

A lot of what you mentioned makes sense and you are right it depends on is its a brand new material. I don’t have a mentor or gifted friend , I just would ask here or on StackOverflow. My question was even more general :

Lets say I’m practicing algorithm and data structure problems - easy/medium level questions that frequently come up in interviews.

How long should I bang my head against the wall until it’s clear I can’t solve it on my own and ask someone to explain it to me? I read people saying never give up or some say some say after 2 hours. Just want to have a time frame so I don’t waste my time.

People please don’t post the solution, I could have just googled it myself :slight_smile:

The point of this discussion is to help me understand the approach to solving these questions

now consider this… do you need to continue looping? or once you have found the right combinatiob you are done?

if numbers[i] + number[j] === target I add it to the array

Then continue but can’t use above combination again.

I just need to make two for loops that check every element but also don’t repeat the same combination.

I don’t know if I explain it well

I just found out my old code doesn’t makes sense.

So I have two loops. second one simply just compares the selected element against rest of the elements to the right. The problem is it compares the same elements against each other which is useless.

To put it differently this loop goes through 12 combinations but it doesn’t have to. Ideally should go through 9 or 6.

Out of 12 three are same element e.g 7&7, 11&11 , 15&15 which is not allowed.
Can’t add same elements together 7+7 =14 is not allowed.

Also it compares some elements twice 7 + 11 and 11 +7 both sum to the same number.

My first loop is correct but the second loop could get better.

Also I need to write a condition so it doesn’t repeat the elements

const array = [2, 7, 11, 15]
const addsUpTo = 9
const limit = array.length
let answer = []

function perfectSumPair (numbers, target) {
    for(let i = 0; i < limit; i++) {
        for(let j = 1; j < limit ; j++) {
            // console.log('i: ', i, 'j: ', j)
            if(numbers[i]+ numbers[j] === target ) {
               answer.push(i, j) 
            }
        }

    }
    return answer
}
console.log(perfectSumPair(array, addsUpTo))