Abhijit chakra
1 min readJun 9, 2021

Airbnb interview question find all pairs of unique indices

https://www.youtube.com/watch?v=dWkA1j-sBPA&t=731s

Here is the full question This problem was asked by Airbnb.

Given a list of words, find all pairs of unique indices such that the concatenation of the two words is a palindrome.

For example, given the list [“code”, “edoc”, “da”, “d”], return [(0, 1), (1, 0), (2, 3)].

Solution

function findUniqueIndices(input){

let map = new Map()
let LTR,RTL

for(let i = 0 ; i< input.length; i++){

if(i === 0){ //i==0
LTR = input[i] + input[i+1] // 0,1
RTL = LTR.split(‘’).reverse().join(“”)
if(LTR === RTL){
map.set(i,i+1)
}

}

if(i === 1){ // i = 2

LTR = input[i] + input[i-1]
RTL = LTR.split(‘’).reverse().join(“”)
if(LTR === RTL){
map.set(i,i-1)
}

}else{ //i=2 (1,2)(2,1)

LTR = input[i-1]+input[i] //(1,2) (2,3)
RTL = LTR.split(‘’).reverse().join(“”)
if(LTR === RTL){
map.set(i-1,i)
}

LTR = input[i]+input[i-1] //(2,1) (3,2)
RTL = LTR.split(‘’).reverse().join(“”)
if(LTR === RTL){
map.set(i,i-1)
}
}

}

return […map.entries()]

}
var input = [“code”, “edoc”, “da”, “d”]
console.log(findUniqueIndices(input))

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response