coding interview-daily

16. storing data log

Problem You run an e-commerce website and want to record the last N order ids in a log. Implement a data structure to accomplish this, with the following API: record(order_id): adds the order_id to the log get_last(i): gets the i-th last e…

43. implementing stack

Problem Implement a stack that has the following method. push(val), which pushes an element onto the stack pop(), which pops off and returns the topmost element of the stack. If there are no elements in the stack, then it should throw an e…

7. パターン数数え

問題 Given the mapping a = 1, b = 2, ... z = 26, and an encoded message, count the number of ways it can be decoded. You can assume that the messages are decodable. For example, '001' is not allowed. 考え方 stringが与えられた時のデコード方…

3. 木構造の実装

問題 Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree. 実装 再帰的に実装します。Noneでなければ展開するようにしています…

5. Pythonでcar,cdrの実装

内容 lispで使うcar, cdrをpythonで実装してみました。 def cons(a, b): def pair(f): return f(a, b) return pair def car(pair): def f(a, b): return a return pair(f) def cdr(pair): def f(a, b): return b return pair(f) if __name__ == "__main__": …

2. arrayからproduct羅列

問題 整数を要素にもつarrayから、どれか1つの要素を除いた積のarrayを出力する。 ただし、割り算は使えない。 考え方 arrayの長さをNとする。 単純に全て計算するとの時間計算量がかかる。 出来るだけ前の値を用いて計算したい。 前の変数までの積と次の変…