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
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.
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.
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))
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.
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!
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.
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.
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.
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
So since there is only one solution, when you find a combination that works you don’t have to continue looping and can return your answer at that point. That will help you avoid some of the unnecessary checks and iterations.
think well also where should j start…
so when i is 0 you want j to start at 1
but when i is 1, you don’t want j to start at 1, as 1-1 is not a valid combination - where should j start here?
when i is 2, where should j start?
not at 0, as 0-2 was already checked, not at 1 as 1-2 was already checked, not at 2 as 2-2 is invalid…