Chào các bạn. Câu hỏi hôm nay như sau: 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 ith last element from the log. i is guaranteed to be smaller than or equal to N. You should be as efficient with time and space as possible.

Đọc tiếp...

Chào các bạn. Câu hỏi hôm nay như sau: Problem This problem was asked by Facebook. Given a stream of elements too large to store in memory, pick a random element from the stream with uniform probability. Vietsub: Cho một file chứa rất lớn số lượng số tự nhiên. Yêu cầu bạn chỉ được đọc qua file 1 lần (sequential read) và phải chọn được một số ngẫu nhiên trong đó sao cho xác suất mỗi phần tử được chọn là bằng nhau, và bằng 1/N, với N là số lượng phần tử trong file.

Đọc tiếp...

Chào các bạn. Câu hỏi hôm nay như sau: Problem The area of a circle is defined as πr^2. Estimate π to 3 decimal places using a Monte Carlo method. Hint: The basic equation of a circle is x2 + y2 = r2. Method Phương pháp Monte Carlo là dựa vào xác suất thống kê để tính toán số Pi. Theo đó thì : Giả sử ta có 1 bảng mục tiêu hình vuông 1x1.

Đọc tiếp...

Chào các bạn. Câu hỏi hôm nay như sau: Problem Given an integer k and a string s, find the length of the longest substring that contains at most k distinct characters. For example, given s = “abcba” and k = 2, the longest substring with k distinct characters is “bcb”.__ Method Ý tưởng của bài này là: Bắt đầu một chuỗi substring đầu tiên là ký tự đầu, sau đó thêm dần các ký tự tiếp của chuỗi sao cho số lượng ký tự unique không vượt quá k.

Đọc tiếp...

Chào các bạn. Câu hỏi hôm nay như sau: Problem There exists a staircase with N steps, and you can climb up either 1 or 2 steps at a time. Given N, write a function that returns the number of unique ways you can climb the staircase. The order of the steps matters. For example, if N is 4, then there are 5 unique ways: 1, 1, 1, 1 2, 1, 1 1, 2, 1 1, 1, 2 2, 2 What if, instead of being able to climb 1 or 2 steps at a time, you could climb any number from a set of positive integers X?

Đọc tiếp...

Chào các bạn. Câu hỏi hôm nay như sau: Problem Implement an autocomplete system. That is, given a query string s and a set of all possible query strings, return all strings in the set that have s as a prefix. For example, given the query string de and the set of strings [dog, deer, deal], return [deer, deal]. Hint: Try preprocessing the dictionary into a more efficient data structure to speed up queries.

Đọc tiếp...

Chào các bạn. Câu hỏi hôm nay như sau: Problem Implement a job scheduler which takes in a function f and an integer n, and calls f after n milliseconds. Code public class Main { public static void main(String[] args) { solution(() -> System.out.println("Hello World"), 10000); } public static void solution(Command command, int n) { new Timer().schedule(new TimerTask() { @Override public void run() { command.execute(); } }, n); } interface Command { void execute(); } } Source code https://github.

Đọc tiếp...

Ảnh đại diện

Tu Bean

Coding, writing and sharing

Web developer

Viet Nam