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

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.

1 Like

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…

do you see the pattern?

Yes I see so j = i + 1 Now it makes sense. :slight_smile: Thank you so much

I updated my code to return the values right away instead of pushing to a new array because as Gcoded and ieahleen mentioned the question said there is exatcly one solution. So as soon as I find the answer I can stop the loop.

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

function perfectSumPair (numbers, target) {
    for(let i = 0; i < limit; i++) {
        for(let j = i + 1; j < limit ; j++) {
            if(numbers[i]+ numbers[j] === target ) {
               return [i, j]
            }
        }
    }
}
console.log(perfectSumPair(array, addsUpTo))

Well, working on programming problems is never a waste of time. If you stick with it and work the problem line by line you come to a solution. As has already been said, write it out, break the problem down into tiny chunks. Google is your friend, search the Internet for multiple solutions and then work on understanding those solutions line by line. You will get your answers. Now, if you have been banging your head for a few hours and not making headway take a break. When I walk away and come back later I usually find a new solution. The important thing is not to give up and get discouraged. Don’t compare yourself to others. If someone solves this problem in a day, great, but it might have been done better. It doesn’t matter to you. You do you and learn from this. The interviewer will be (or should be ) more interested in your thought process and approach to the problem and not how fast you banged it out. Just know your solution and be prepared to explain it when you go back to the interview. Good luck with the job!

1 Like

and what happens if you call the array with something like

perfectSumPair([2,6,12,1,3,4,1,8],9)

try calling it just below your current one

what result do you get?
is it the right one?

1 Like

and what happens if you call the array with something like

perfectSumPair([2,6,12,1,3,4,1,8],9)
what result do you get?

I get the correct result: [1, 4]

function perfectSumPair (numbers, target) {
    const limit = numbers.length
    for(let i = 0; i < limit; i++) {
        for(let j = i + 1; j < limit ; j++) {
            if(numbers[i]+ numbers[j] === target ) {
               return [i, j]
            }
        }

    }
}
console.log(perfectSumPair([2,6,12,1,3,4,1,8], 9))

well, with the code you had before you wouldn’t, hou had hardcoded limit with a very specific value

good job :+1:

1 Like

Yes, limit should have been inside the function and not hardcoded.

This version is much better.

Thank you for being patient and asking the right questions :slightly_smiling_face:

I made this get unstuck checklist, it has 9 steps, based on your comments. Did I miss anything let me know.

Finally, thank you all for helping :love_you_gesture::love_you_gesture:

  1. Make sure to understand the conditions of the problem
    Write out in plain words what algorithm should do
  1. Write the algorithm
  2. Go through what each line suppose to do
  3. console.log results, even if it’s a loop
  1. What data console.log returns?
  2. What data I suppose to get; This step helped me a lot
  1. Test it with different inputs
  1. Take a break come back to it again.

  2. Ask for help on FCC forum or stackoverflow

and do all the testing again, try to break it, try to find an input that doesn’t work

1 Like

According to the problem description:

Given an array of sorted integers,

The elements are always going to be sorted, so the above call would not be made.

@Realitydistortionfie It is the fact that the elements of the array are integers and that they are sorted, which makes it possible to solve this with a very simple algorithm.
I used a single for loop to solve this problem.

As I iterated through the array of integers, I calculated the matching pair number by subtracting the current number being iterated over from the target. I used an object to track the index of the current number as it related to the matched number (property name = matched number and the property value = index). If my object contained the matched property, then I know that I have found my two indices (the index of the current number and the index of the matched number property in my object. Then, I just return an array of the two indices and the function exits. If the matched pair number is not found in my object, I add it as a new property with its value equal to the current number’s index.

1 Like

I just wanted to chime in as an educator and employer and say that “learning is not cheating”. If they wanted to make sure that you knew it then they would have asked you to solve it during an online interview while they watch you and your screen. The fact that they gave you the problem to do at your own pace at home says that they didn’t expect you to know the answer but they want someone who can understand it. So learn about it, ask questions about it, dive into why are some answers correct and why other answers are wrong. Expect that in the next part of the interview they ask you to explain the meaning and function behind each of the problems given. Why did you choose the algorithm you chose for the answer? Things like that.

Copy/Paste answers are cheating (I get a lot of that teaching in Malaysia). But using the internet to do research and understand something is not cheating. So first go to YouTube and watch as many videos as you need to about the subject, solve for it, then ask questions here or on stack about the parts that you don’t understand.

And yes, the faster you get back to them the better.

Good luck and hope this helped.

1 Like

Ok guys, I was working on the second question for past two days :sweat_smile:

Jewel Spelunker​
Given a cave represented by a grid of size ​m * n​, find the most jewels a spelunker can collect. Each cell in this cave has an integer representing the number of jewels in that cell, 0 if it is empty.
Return
the maximum amount of jewels you can collect under the conditions:
● Every time you go through a cell you will collect all the jewels in that cell.
● From your position, you can walk one step to the left, right, up or down. (No diagonal
movement.)
● You can’t visit the same cell more than once.
● Never visit a cell with 0 jewels.
● Your starting point is always at 0,0 and this item always contains 1 jewel.

Example 1: 
Input:​ grid = [
[ 1, 6, 0 ], 
[ 5, 8, 7 ], 
[ 0, 9, 0 ]] 
Output:​ ​24

Explanation:​ ​1 ⇒ 6 ⇒ 8 ⇒ 9. Remember: you can’t go back through 
a cell you already visited and you can’t go through a cell with 
the value 0.

Example 2:
Input:​ grid = [
[ 1, 2, 3, 4, 100 ], 
[ 10, 0, 0, 0, 150 ], 
[ 200, 0, 0, 0, 0 ],
] 
Output:​ ​260

Explanation:​ While going ​1 ⇒ 10 ⇒ 200 (211) is tempting, 
the best route is 1 ⇒ 2 ⇒ 3 ⇒ 4 ⇒ 100 ⇒ 150 (260)

I mapped out what the algorithm should do step by step in plain words.

  1. Make two loops(outer and inner)
  2. Which positions from (0,0) we can move to
  3. Eliminate invalid positions [zero, previously visited, wrong positions ]
  4. Show valid positions
  5. Select the position with bigger value, if equal pick any
  6. Move to the position
  7. repeat 3,4,5, until there is no valid position
  8. End of inner loop
  9. store positions visited, total value of cells
  10. This is the end of path 1
  11. repeat creating paths until there is no new path to be added( I don’t know to code this)
  12. end of outer loop
  13. compare value of cell in all paths and return the biggest one

Basically brute force method. I took a long time to write the map out this and even longer to code it. My code stores the information for positions in movement_data object. Then there are two while loops. One will repeat until we can’t move to any new position. The other while should repeat until we traveled every combination possible.

My main problem is I don’t know how to make condition for second while loop so it will stop after every combination is traveled. Also I have alot of syntax errors for accessing objects. Also I only did Left property I will add the other ones. Anyway its late now so I will just wanted to see your thoughts about the general approach and if using objects was a good idea. Tomorrow will post a better and polished code.

function getJewels(grid) {

        let movement_data = {
            current_position: {m: 0, n:0},
            path: {
                    0 : {
                        val_jewel: [1],
                        route: {  0: { m: 0, n:0  } }
                        }
            }
        }
        while(Valid_positions.Left.valid === true || Valid_positions.right.valid === true ||Valid_positions.up.valid === true || Valid_positions.down.valid === true) {

            let Valid_positions = { // checks all directions for valid positions

            Left: {   
                position: { m : movement_data.current_position.m  , n :movement_data.current_position.n -1 },
                value: grid[this.position.m][this.position.n],
                positionAllowed () { 
                    if(this.position.m >= 0 && this.position.n >= 0 && this.position.m  <= grid[0].length - 1 && this.position.n <= grid.length -1 ){
                        return true
                    }else {
                        return false
                    }
                },
                NotZero() {
                    if(this.value !== 0){
                        return true
                    }else{
                        return false
                    }
                },
                notVisited() {  //Correct i 
                    let limit = Object.keys(movement_data.path[pathNumber].route).length
                    for( let i = 0; i < limit; i++) {
                        if(movement_data.path[pathNumber].route[i].m === this.position.m && movement_data.path[pathNumber].route[i].n === this.position.n ){
                            return false
                        }else{
                            return true
                            }
                        }
                    },
                valid(){
                    if(this.positionAllowed() && this.NotZero() && this.notVisited() === true ){
                        return true
                    }else{
                        return false
                    }
                }    
                }

            },
        }


        let whereToGO = []
        let MoveToValidPosition = ()=> {  // chooses a valid position and moves there 

            for(let i = 0; i < Valid_positions.length; i++){ 
                if(Valid_positions[i].valid === true){
                    whereToGO.push(Valid_positions[i].value) // if direction is valid, add it to wheretoGO
                }
            }

            if(whereToGO.length !==0 ) {
            for(let i = 0; i < Valid_positions.length; i++){
                if( Math.max(whereToGO) === Valid_positions[i].value && Valid_positions[i].valid === true){
                    movement_data.current_position.m = Valid_positions[i].position.m                            // updated m  current position
                    movement_data.current_position.n = Valid_positions[i].position.n                            // updated n current position 
                    movement_data.path[0].route[movement_data.route.length].m = Valid_positions[i].position.m   // updated m  visted position
                    movement_data.path[0].route[movement_data.route.length].n = Valid_positions[i].position.n   // added n visted position
                    movement_data.val_jewel = [...movement_data.val_jewel, Valid_positions[i].value]            //  updated jewel value
                }

            }
        }else{
            pathNumber++
            movement_data.path[pathNumber].val_jewel = [1]
            movement_data.path[pathNumber].route[0].m = 0
            movement_data.path[pathNumber].route[0].n = 0 

        }
        }
    }
       
        console.log(Left)
    }
}

console.log(getJewels([
    [ 1, 6, 0 ], 
    [ 5, 8, 7 ], 
    [ 0, 9, 0 ]
    ]))

This code is makes more sense now. But I need to fix few errors.

function getJewels(grid) {

    let movement_data = {
        // current_position: {m: 0, n:0},
        // path: {
        //         0 : {
        //             val_jewel: [1],
        //             route: {  0: { m: 0, n:0  } }
        //             }
        // }
    }
        // Condition to explore all paths 
        let newPathpossible = true
        let pathNumber = 0
        let routeNumber = 0

        while(newPathpossible === true){
        
        // In Object movement_data, create properties: current position, path[val_jewel], path[route][m] path[route][n] 
        movement_data.current_position.m = 0
        movement_data.current_position.n = 0
        movement_data.path[pathNumber].val_jewel = [1]
        movement_data.path[pathNumber][routeNumber].m = 0
        movement_data.path[pathNumber][routeNumber].n = 0
        
        let movedToNewPosition = true
        
        // Whether to go left, right, up or down untill in reaches the end 

        while(movedToNewPosition === true) {
        
            let Valid_positions = {}
            
            // loop to create 4 properties left, right, up and down in Valid_positions object
            
            // add position and value property to left (0)
            Valid_positions[0].position.m = movement_data.current_position.m
            Valid_positions[0].position.n = movement_data.current_position.n - 1
            Valid_positions[0].value = grid[movement_data.current_position.m][movement_data.current_position.n]
            
            // add position and value property to right (1)
            Valid_positions[1].position.m = movement_data.current_position.m
            Valid_positions[1].position.n = movement_data.current_position.n + 1
            Valid_positions[1].value = grid[movement_data.current_position.m][movement_data.current_position.n]
            
            // add position and value property to up (2)
            Valid_positions[2].position.m = movement_data.current_position.m - 1
            Valid_positions[2].position.n = movement_data.current_position.n
            Valid_positions[2].value = grid[movement_data.current_position.m][movement_data.current_position.n]
           
            // add position and value property to down (3)
            Valid_positions[3].position.m = movement_data.current_position.m + 1
            Valid_positions[3].position.n = movement_data.current_position.n
            Valid_positions[3].value = grid[movement_data.current_position.m][movement_data.current_position.n]

            for(let j = 0; j < 4; j++) {

            function positionAllowed(){
                if(Valid_positions[j].position.m >= 0 && Valid_positions[j].position.n >=0 && Valid_positions[j].position.m <= grid[0].length -1 && Valid_positions[j].position.n <= grid.length - 1 ){
                    Valid_positions[j].positionAllowed = true
                }
                Valid_positions[j].positionAllowed = false
            }

            function notZero() {
                if(Valid_positions[j].value !== 0){
                    Valid_positions[j].notZero = true
                }
                Valid_positions[j].notZero = false
            }   

            function notVisited() {
                let limit = Object.keys(movement_data.path[pathNumber].route)
                for(let i = 0; i < limit; i++){
                    if(Valid_positions[j].position.m !== current_position.path[pathNumber].route[i].m && Valid_positions[j].position.n !== current_position.path[pathNumber].route[i].n){
                        Valid_positions[j].notVisited = true 
                    }
                    Valid_positions[j].notVisited = false
                }
            }

            function valid() {
                if(Valid_positions[j].positionAllowed === true && Valid_positions[j].notZero === true && Valid_positions[j].notVisited === true ){
                    Valid_positions[j].valid = true 
                }
                    Valid_positions[j].valid = false 
            }
        } // end of loop j for repeating functions
        
        // Object Valid_positions now has validated all four directions 

        
        let whereToGo = {}

        // choose from Valid_positions and moves there
        
        let MoveToValidPosition = ()=> {   
            let n = 0   
            
            for(let i = 0; i < Object.keys.Valid_positions.length; i++){ 
                if(Valid_positions[i].valid === true) {
                    // add position's value and position to whereToGo Object
                    whereToGo[n].value = Valid_positions[i].value
                    whereToGo[n].position.m = Valid_positions[i].position.m
                    whereToGo[n].position.n = Valid_positions[i].position.n
                    n++
                }

            }
        }

            // Now we whereToGo Object has all valid directions or has nothing which means its end of path
            // select the bigger value. Go to that cell and update positions

            let whereToGoBiggestvalue = whereToGo[0].value
            let whereToGoBiggestM = null 
            let whereToGoBiggestN = null 
            if(Object.keys.whereToGo.length !== 0) {
                
                // select the bigger value
                // if value previously selected go to the next value
                // pick bigger value
                for(let l = 1; l < Object.keys.whereToGo.length; l++) {
                    if(whereToGo[l].value > value){
                        value = whereToGo[l].value
                        whereToGoBiggestM = whereToGo[l].position.m
                        whereToGoBiggestN = whereToGo[l].position.n
                    }
                }
                movement_data.current_position.m = whereToGoBiggestM                        // updated m  current position
                movement_data.current_position.n = whereToGoBiggestN                           // updated n current position 
                movement_data.path[pathNumber].route[routeNumber].m = whereToGoBiggestM      // updated m  visted position
                movement_data.path[pathNumber].route[routeNumber].n = whereToGoBiggestN    // added n visted position
                movement_data.val_jewel = [...movement_data.val_jewel, whereToGoBiggestvalue] 

            }else if(Object.keys.whereToGo.length === 1){
                whereToGoBiggestvalue = whereToGo[0].value
                whereToGoBiggestM = whereToGo[0].position.m
                whereToGoBiggestN = whereToGo[0].position.n

                movement_data.current_position.m = whereToGoBiggestM                        // updated m  current position
                movement_data.current_position.n = whereToGoBiggestN                           // updated n current position 
                movement_data.path[pathNumber].route[routeNumber].m = whereToGoBiggestM      // updated m  visted position
                movement_data.path[pathNumber].route[routeNumber].n = whereToGoBiggestN    // added n visted position
                movement_data.val_jewel = [...movement_data.val_jewel, whereToGoBiggestvalue] 
             
            } else if(Object.keys.whereToGO.length === 0){
                movedToNewPosition = false
                /* No move moves so end of route, End of inner loop, now check in this route doesn't exist in any path*/
                
                // check if route already exists in any paths before adding
                for(let h = 0; h < Object.keys.movement_data.path[l].route;l++){
                    for(let p = 0; Object.keys.movement_data.path[l].route[p].length; p++){}
                    // if below translate: if its exatly like a path but its not the same path im adding, then this path has been added  
                        if(movement_data.path[l].route[p].m === movement_data.path[pathNumber].route[p].m && movement_data.path[l].route[p].n === movement_data.path[pathNumber].route[p].n && movement_data.path[l].val_jewel === movement_data.path[pathNumber].val_jewel && l !== pathNumber) {
                            newpathtaken = false
                        }
                        else{
                            newPathpossible = true
                            pathNumber++
                        }
                }
                

            }
    
            }   // where to close first while loop

            // close while loop newPathpossible 
        }
    }

    // return biggest path
    let answer = 0
    for(let x = 0; x < Object.keys.movement_data.path; x++){
        if(movement_data.path[x].val_jewel > answer){
            answer = movement_data.path[x].val_jewel
        }
    }

    return answer


       
    }
}

console.log(getJewels([
    [ 1, 6, 0 ], 
    [ 5, 8, 7 ], 
    [ 0, 9, 0 ]
    ]))

Ok I made it all the way and I’m stuck in line 192 Reference error h is not defined.
This code is so long so Let me give a summary;

start at 0,0
make decision to go left, right, up or down (I know technically I can go only right or down at the start but I made a code to fit every movement after ).
I apply restrictions can’t go to cell 0, can’t be outside grid e.g -1, 0 and can’t go to already traveled cells.

After restrictions check valid options I have
I move to option with biggest value.
This repeats using while loop ‘movedToNewPosition’.
If can’t go to new position we reached end of route (combination of positions taken until can’t go anywhere ).
Then we go back to 0,0 again using the outer while loop (newPathpossible)
This time we will try a different route
we repeat until we have taken every path possible

thats where I'm stuck line 192.
I made two variables one h checks every path and p check routes within the selected path.

I want to check if that exact route has been taken by another path, if it hasn’t add that to movement_data object and repeat creating paths.
If not it means we have taken every path possible and thus end of outer while loop .

function getJewels(grid) {

    let movement_data = {
        current_position: {m: 0, n:0},
        path: {
                0 : {
                    val_jewel: 1,
                    route: {  0: { m: 0, n: 0 } }
                    }
        }

    }
        // Condition to explore all paths
        let newPathpossible = true
        let pathNumber = 0
        
        while(newPathpossible === true) {
        let routeNumber = 0
        // In Object movement_data, create properties: current position, path[val_jewel], path[route][m] path[route][n] 
        movement_data.current_position.m = 0
        movement_data.current_position.n = 0
        movement_data.path[pathNumber] = {}
        movement_data.path[pathNumber].val_jewel = 1
        movement_data.path[pathNumber].route = {}
        movement_data.path[pathNumber].route[routeNumber] = {}
        movement_data.path[pathNumber].route[routeNumber].m = 0
        movement_data.path[pathNumber].route[routeNumber].n = 0
        // console.log("movement_data: ", Object.getOwnPropertyNames(movement_data.path[pathNumber].route[routeNumber]))
        let movedToNewPosition = true
        
        // Whether to go left, right, up or down untill in reaches the end 
        // maybe second while is not needed because as soon as new path is added, the loop is finish and return back where current position is 0,0
        while(movedToNewPosition === true) { // movedToNewPosition while
        
            let Valid_positions = { 
                0: {},
                1: {},
                2: {},
                3: {}
            }
            
            // loop to create 4 properties left, right, up and down in Valid_positions object
            
            // add position and value property to left (0)
           
            Valid_positions[0].position = {}
            Valid_positions[0].position.m = movement_data.current_position.m
            Valid_positions[0].position.n = movement_data.current_position.n - 1
            if(Valid_positions[0].position.m <= 0 && Valid_positions[0].position.n <=0 || Valid_positions[0].position.m > grid[0].length -1 || Valid_positions[0].position.n > grid.length - 1){
            Valid_positions[0].positionAllowed = false
            }else{
            Valid_positions[0].value = grid[Valid_positions[0].position.m][Valid_positions[0].position.n]
            Valid_positions[0].positionAllowed = true
            }
            // add position and value property to right (1)
            
           
            Valid_positions[1].position = {}
            Valid_positions[1].position.m = movement_data.current_position.m
            Valid_positions[1].position.n = movement_data.current_position.n + 1
            if(Valid_positions[1].position.m <= 0 && Valid_positions[1].position.n <=0 || Valid_positions[1].position.m > grid[0].length -1 || Valid_positions[1].position.n > grid.length - 1){
            Valid_positions[1].positionAllowed = false
            }else{
            Valid_positions[1].value = grid[Valid_positions[1].position.m][Valid_positions[1].position.n]
            Valid_positions[1].positionAllowed = true
            }   
            // add position and value property to up (2)

            
            Valid_positions[2].position = {}
            Valid_positions[2].position.m = movement_data.current_position.m -1
            Valid_positions[2].position.n = movement_data.current_position.n
            if(Valid_positions[2].position.m <= 0 && Valid_positions[2].position.n <=0 || Valid_positions[2].position.m > grid[0].length -1 || Valid_positions[2].position.n > grid.length - 1){
            Valid_positions[2].positionAllowed = false
            }else{
            Valid_positions[2].value = grid[Valid_positions[2].position.m][Valid_positions[2].position.n]
            Valid_positions[2].positionAllowed = true
            }
            // add position and value property to down (3)
           
            Valid_positions[3].position = {}
            Valid_positions[3].position.m = movement_data.current_position.m + 1
            Valid_positions[3].position.n = movement_data.current_position.n
            if(Valid_positions[3].position.m <= 0 && Valid_positions[3].position.n <=0 || Valid_positions[3].position.m > grid[0].length -1 || Valid_positions[3].position.n > grid.length - 1){
            Valid_positions[3].positionAllowed = false
            }else{
            Valid_positions[3].value = grid[Valid_positions[3].position.m][Valid_positions[3].position.n]
            Valid_positions[3].positionAllowed = true
            }

            console.log("Object ValidPositions: ", Valid_positions)

            for(let j = 0; j < 4; j++) { // for loop j
            function notZero() {
                if(Valid_positions[j].value !== 0){
                    Valid_positions[j].notZero = true
                }
                Valid_positions[j].notZero = false
            }   
            notZero()
            
            
            function notVisited() {
                let limit = Object.keys(movement_data.path[pathNumber].route).length
                for(let i = 0; i < limit; i++){
                    if(Valid_positions[j].position.m !== movement_data.path[pathNumber].route[i].m && Valid_positions[j].position.n !== movement_data.path[pathNumber].route[i].n){
                        Valid_positions[j].notVisited = true 
                    }
                    Valid_positions[j].notVisited = false
                }
            }
            notVisited()

            function valid() {
                if(Valid_positions[j].positionAllowed === true && Valid_positions[j].notZero === true && Valid_positions[j].notVisited === true ){
                    Valid_positions[j].valid = true 
                }
                    Valid_positions[j].valid = false 
            }
            valid()
        } // closed for loop j

        // Object Valid_positions now has validated all four directions 
        
        // choose from Valid_positions and move there
        let whereToGo = {}
        
        let MoveToValidPosition = ()=> {   
            let n = 0   
            // could have checked in valid postions instead of making wheretoGo object
            //  limit could have been Object.keys.Validpostions.valid.true.length
            let limit = Object.keys(Valid_positions).length
            console.log("LIMIT: ",limit)
            for(let i = 0; i < limit; i++){ 
                if(Valid_positions[i].valid === true) {
                    // add position's value and position to whereToGo Object
                    whereToGo[n].value = Valid_positions[i].value
                    whereToGo[n].position.m = Valid_positions[i].position.m
                    whereToGo[n].position.n = Valid_positions[i].position.n
                    n++
                }

            }
        }
        MoveToValidPosition()

            // Now  whereToGo Object has all valid directions or has nothing which means its end of path
            // select the bigger value. Go to that cell and update positions
            // select the bigger value
            // doesnt need to check for previously becaus it wouldn't come up
            // it didn't check the path so far so it wants to take the same path again and it won't know untill the end 
            if(Object.keys(whereToGo).length !== 0) { // if there are available position to go to
                let whereToGoBiggestvalue = whereToGo[0].value
                let whereToGoBiggestM = whereToGo[0].position.m
                let whereToGoBiggestN = whereToGo[0].position.n
                
                for(let l = 1; l < Object.keys(whereToGo).length; l++) {
                    if(whereToGo[l].value >= whereToGoBiggestvalue){
                        whereToGoBiggestvalue = whereToGo[l].value
                        whereToGoBiggestM = whereToGo[l].position.m
                        whereToGoBiggestN = whereToGo[l].position.n
                    }
                }
                routeNumber++
                movement_data.current_position.m = whereToGoBiggestM                        // updated m  current position
                movement_data.current_position.n = whereToGoBiggestN                           // updated n current position 
                movement_data.path[pathNumber].route[routeNumber].m = whereToGoBiggestM      // updated m  visted position
                movement_data.path[pathNumber].route[routeNumber].n = whereToGoBiggestN    // added n visted position
                movement_data.val_jewel = movement_data.path[pathNumber].val_jewel + whereToGoBiggestvalue
                

            }else if(Object.keys(whereToGo).length === 1){ // if one position to go to
                let whereToGoBiggestvalue = whereToGo[0].value
                let whereToGoBiggestM = whereToGo[0].position.m
                let whereToGoBiggestN = whereToGo[0].position.n

                movement_data.current_position.m = whereToGoBiggestM                        // updated m  current position
                movement_data.current_position.n = whereToGoBiggestN                           // updated n current position 
                movement_data.path[pathNumber].route[routeNumber].m = whereToGoBiggestM      // updated m  visted position
                movement_data.path[pathNumber].route[routeNumber].n = whereToGoBiggestN    // added n visted position
                movement_data.val_jewel = movement_data.val_jewel + whereToGoBiggestvalue
                routeNumber++
            }else if(Object.keys(whereToGo).length === 0){
                movedToNewPosition = false
                /* No move moves so end of route, End of inner loop, now check in this route doesn't exist in any path*/
                
                // check if route already exists in any paths before adding
                // movement_data.path[l].route[i].m === movement_data.path[2].route[i].m no
                // movement_data.path[l].route[i].n === movement_data.path[2].route[i].n yes
                //  if there is a single no, it means that routes are not identical
                let limitH = Object.keys(movement_data.path).length
            

                for(let h = 0; h < limitH; h++) {
                    for(let p = 0; p < Object.keys(movement_data.path[h].route).length; p++) {
                    // below translate: if its exatly like a path but its not the same path im adding, then this path has been added  
                        if(movement_data.path[h].route[p].m === movement_data.path[pathNumber].route[p].m && movement_data.path[h].route[p].n === movement_data.path[pathNumber].route[p].n && movement_data.path[h].val_jewel === movement_data.path[pathNumber].val_jewel && h !== pathNumber) {
                            newPathpossible = false
                        }
                        else{
                            newPathpossible = true
                            pathNumber++
                        }
                    }
                }
                

            }   // last else if closed
    
         }   // movedToNewPosition while loop closed
            
    }     // newPathpossible while loop closed
    

    // return biggest path
    let answer = 0
    for(let x = 0; x < Object.keys(movement_data.path).length; x++){
        if(movement_data.path[x].val_jewel >= answer){
            answer = movement_data.path[x].val_jewel
        }
    }

    return answer

}

console.log(getJewels([
    [ 1, 6, 0 ], 
    [ 5, 8, 7 ], 
    [ 0, 9, 0 ]
    ]))

Hey, I just read your second problem and I can say now that I cannot solve it right now.
But I think you should have another approach to this problem.
First of all having so many lines for one problem usually means that you’re getting further away from the solution and probably adding a ton of complexity where it shouldn’t.
That being said, I think you look for different type of data structure, may be a tree like.
I’m really not sure but I think a tree object that holds nodes which each of them contains a value, the next node on the left and the next node on the right.
You’ll find lots of content on the web about that.

1 Like

I found the question on Leetcode Path with Maximum gold small difference is instead of 0,0 we are allowed to start anywhere. It’s medium difficulty question.

I’ve been working on it for 8 days so I will try different approach. I will look at the solution and try to figure out what they did.

This question is was very hard for me. Should an average junior developer be able to solve this?

Well it looks hard to me as well. Some interview questions could be testing how effective you are at searching solutions to problems you have never encountered before. Probably that is why it is a take-home interview question. If you have been solving it for 8 days, i guess you can look at the solutions of other people, and come up with your own code after understanding their approach.