Last week had a phone interview for a front-end position. The interviewer asked two questions. The first question was to write a function that returns an array of objects. The input should match the expected output.
const input = [
{
"value": "value0",
"children": []
},
{
"value": "value1",
"children": [
{
"value": "value2",
"children": [
{
"value": "value3",
"children": []
}
]
},
{
"value": "value4",
"children": []
}
]
},
{
"value": "value5",
"children": []
},
{
"value": "value6",
"children": []
}
];
const expected = [
{"value":"value0"},
{"value":"value1"},
{"value":"value2"},
{"value":"value3"},
{"value":"value4"},
{"value":"value5"},
{"value":"value6"}
];
The second question was an extension of the first. It required writing a getValueList(fromIndex, toIndex)
function that calls getBatch
, which returns a promise. When calling getValueList
as shown below, the result should match the previous question’s result (you can use the first question as a helper function).
getValueList(1, 3).then(result => console.log(result));
const expected = [
{"value":"value0"},
{"value":"value1"},
{"value":"value2"},
{"value":"value3"},
{"value":"value4"},
{"value":"value5"},
{"value":"value6"}
];
const getBatch = (index) => {
return new Promise((resolve, reject) => {
if (index === 1) {
resolve([
{
value: "value0",
children: []
}
]);
} else if (index === 2) {
resolve([
{
value: "value1",
children: [
{
value: "value2",
children: [
{
value: "value3",
children: []
}
]
},
{
value: "value4",
children: []
}
]
},
{
value: "value5",
children: []
}
]);
} else if (index === 3) {
resolve([
{
value: "value6",
children: []
}
]);
}
});
};