Перейти к содержимому

puzzles

2 posts with the tag “puzzles”

Solved all React challenges on Hackerrank and not satisfied

I read that hakerrank has challenges organized by domain — for example, the React domain — and decided to solve them as part of my interview preparation.

I completed all of them, but not satisfied. Here’s why:

  1. The challenges don’t feel like real interviews. They give you a lot of code, while in actual interviews you often write components almost from scratch.
  2. The tests are not reliable. In one case, I had to remove the key from react element just to make their test pass. Test kept a reference to the element at the start and didn’t update it after re-render. In another challenge, tests used hardcoded outdated dates, so I had to add a weird condition just to pass.
  3. The platform itself has issues. Sometimes my progress wasn’t saved. A couple of times I submitted a solution, saw the “Congratulations” screen, went back to the challenge list — and it showed the challenge as unsolved. I had to redo the solution. Eventually, I started double-clicking the submit button just in case, because I didn’t want to retype everything again.

Overall, I regret the time spent a little, because mostly I fought with tests, not with solving problem. Moreover, challenges turned out to be not so interesting. I liked leetcode experience more.

Hakerrank page with completed challenges

Boxes in boxes, Или что застряло у меня в голове

Я периодически решаю задачи на leetcode или codewars, и недавно наткнулась на кату “Boxes in boxes”.

Это вроде бы не то чтобы сложная задача, но я застряла. Для начала, у меня есть проблемы с пространственным мышлением, так что я не сразу поняла сам паттерн с рисованием этих ящиков.

И даже когда я поняла паттерн, я… не смогла написать решение. Понимала, что нужно рисовать ящики от первого и затем с повторением уже существующих нарисованных, но как? Это просто не укладывалось в моей голове.

После двух вечеров размышлений, я была вынуждена посмотреть чужие решения (эта задача просто сводила меня с ума). И знаете что? Даже после того, как я увидела код и поняла, что он должен делать, я так и не могла осознать, а каким же образом это приводит нас к нужному результату.

В итоге я написала свой вариант, основываясь на идеях из одного решения, и дебажила его, пока вроде не поняла, как именно мы рисуем эти ящики.

Оставлю мой код с комментами здесь, чтобы если можно было вспомнить ход мысли:

function draw(n) {
// all for one box, our start
let res = [" _ ", "|_|"];
for (let i = 1; i < n; i++) {
res = [
// top line - just add tops of boxes
' _' + res[0],
// draw existing boxes without left border (top 1/2 part is repeat existing but partially)
...res.slice(1).map(str => '| ' + str.slice(1)),
// draw existing with left border and without bottom
...res.slice(1, -1).map(str => '| ' + str),
// draw bottom
'|_' + res[res.length-1],
]
}
return res.join('\n');
}

Остаётся только надеяться, что на интервью мне эта задача не попадётся 🤪