Flipkart interview experience.
ds questions
Q1
Given an unsorted array with both positive and negative elements you have to find the smallest positive number missing from the array.
Input: {2, 3, 7, 6, 8, -1, -10, 15}
Output: 1
Input: { 2, 3, -7, 5, 8, 1, -10, 15, 4 }
Output: 6
Input: {1, 1, 0, -1, -2}
Output: 2
— — — — — — — — — -
Input type arr
OUtput — smallest positive number
Var temp = 1
Var threshold = {}
for(let i=0; i< arr.length; i++){
if(arr[i] > 0) {
if(Threshold[arr[i]]){
Threshold[arr[i]] = ++Threshold[arr[i]]
} else {
Threshold[arr[i]] = 1
}
} else {
continue
}
}
for(var val in threshold) {
if(threshold[temp]) {
temp ++
} else {
cons
}
}
Q2.
Given a string of the form abc{1}p{2}, expand the string. Remove the numbers, & repeat the string before the number as many times as the number enclosed in {}
- Input — ab{2}c Output — ababc
- Input — xy{2}bd{2} Output — xyxybdxyxybd
- Input — a{3}b{2}c{3} Output — aaabaaabcaaabaaabcaaabaaabc
— — — — —
Abc[2]ab -
Let temp = “”
for(var val in input){
if(val !== ‘{’)
Temp = temp + val
} else {
Const indexOfNumber = val.indexOf(‘{’)
Let numIndex = indexOfNumber + 1
for(let i = 0; i<input[numIndex]; i++){
Temp = temp + temp
}
}
}