注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
Fast.co 面试,用的 Karat
先是5题像是 system design的题。
1. 如果有200个相同的 Instance,每过几分钟就会有一台机器宕机。 问都有可能是什么原因
2. 一个图里有 http code 2xx, 3xx, 4xx, 5xx,的几条线,纵坐标是 query per second, 5xx 的线有几个 peak。问发生了什么
3. 一个图显示有API latency, 问怎么办
两题 coding:
reflow字符串
1. word wrap
给一个word list 和最大的长度,要求把这些word用 - 串联起来,但不能超过最大的长度。
We are building a word processor and we would like to implement a "word-wrap" functionality.
Given a list of words followed by a maximum number of characters in a line, return a collection of strings where each string element represents a line that contains as many words as possible, with the words in each line being concatenated with a single '-' (representing a space, but easier to see for testing). The length of each string must not exceed the maximum character length per line.
Your function should take in the maximum characters per line and return a data structure representing all lines in the indicated max length.
Examples:
words1 = [ "The", "day", "began", "as", "still", "as", "the",
"night", "abruptly", "lighted", "with", "brilliant",
"flame" ]
wrapLines(words1, 13) "wrap words1 to line length 13" =>
[ "The-day-began",
"as-still-as",
"the-night",
"abruptly",
"lighted-with",
"brilliant",
"flame" ]
wrapLines(words1, 20) "wrap words1 to line length 20" =&gi < words.length) {
if (remain - words[i].length < 0) {
break;
}
count++;
remain -= words[i++].length + 1;
}
const line = words.slice(i - count, i);
// after splitting into lines, calculate the required dashes between each word
const n = line.reduce((n, word) => n + word.length, 0);
let reflowed = ''; // line result with padded dashes
const baseDash = '-'.repeat(parseInt((maxLen - n) / (line.length - 1)));
let extra = (maxLen - n) % (line.length - 1); // some dashes at the beginning has one extra dash
for (let j = 0; j < line.length; j++) {
if (j === line.length - 1) {
reflowed += line[j];
} else {
reflowed +=
extra-- <= 0 ? line[j] + baseDash : line[j] + baseDash + '-';
}
}
result.push(reflowed);
}
return result;
}
|