banner
cos

cos

愿热情永存,愿热爱不灭,愿生活无憾
github
tg_channel
bilibili

2022 Spring JD Front-end Written Test Review

Review of JD written test questions, including multiple-choice questions (30) and two programming questions (which happened to be on the same day as the 360 test, what a coincidence, two tests in one day).

Multiple-choice Questions (30)#

Covering various topics, such as databases, calculus, and equations, with a focus on frontend questions in the second half.

Programming Question 1: Maximum Value for Xiao Ming (AC100%)#

Xiao Ming has a machine that returns a non-negative integer every time he inputs a number. After multiple experiments, he has learned that if he gives the machine a number x, the machine will return the remainder y when x is divided by P. The value of P is engraved on the machine, and Xiao Ming can see it.

For example, when P=5, inputting x=9 will return 4, and inputting 15 will return 0.

Now Xiao Ming can input all the integers in the closed interval [L,R]. He wants to know the maximum value he can obtain from this machine.

Input Description:
There are multiple test cases. The first line contains a number T, indicating the number of test cases.

The next three lines each contain T integers L[i], R[i], P[i], where L[i], R[i], P[i] represent the left endpoint, right endpoint, and parameter P of the i-th test case.

Output Description:
Output a line containing T numbers, representing the answers for each test case in order.

Sample Input:

2
5 1
6 2
5 7

Sample Output:

1 2

Hint:
For the first test case, inputting 5 will return 0, and inputting 6 will return 1, so the answer is 1.

For the second test case, inputting 1 will return 1, and inputting 2 will return 2, so the answer is 2.

Approach#

It is obvious that the task is to find a number x in the range L to R such that x%p is maximized. To solve this, check if L/p is equal to R/p. If they are equal, the maximum remainder is R%p. Otherwise, it means that a full cycle has already been completed, and the maximum remainder is p-1.

Code#

#include <iostream>
#include <vector>
using namespace std;
const int maxn = 70005;
int T;
int L[maxn], R[maxn], P[maxn];
vector<int> ans;
int main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin >> T;
    for(int i = 0; i < T; ++i) 
        cin >> L[i];
    for(int i = 0; i < T; ++i) 
        cin >> R[i];
    for(int i = 0; i < T; ++i) 
        cin >> P[i];
    for(int i = 0; i < T; ++i) {
        int l = L[i], r = R[i], p = P[i];
        if(l/p == r/p) ans.push_back(r%p);
        else ans.push_back(p-1);
        
    }
    for(int i = 0; i < T; ++i) {
        if(i == 0) cout << ans[i];
        else cout << ' ' << ans[i];
    }
    cout << endl;
    return 0;
}

Programming Question 2: Egg Division (AC 73%)#

In simple terms:
Starting with x, you can perform the following two operations:

  • ++x
  • if(x%3 == 0) x /= 3
    How many steps are needed to transform x into y with the minimum number of steps?

Here are some test cases:
Sample 1:

3
102 1
312 12
23 10
10
5
4

Sample 2:

4
210 4
121 3
312 102
281 200
8
10
70
108

Sample 3:

4
299 298
31 100
8 1
900100000000000000 20000200000100000
200
69
3
8887854321087664

Now let's analyze the problem.

Approach#

Try to divide x by 3 as much as possible. If x/3 is less than y, then increment x until it becomes divisible by 3 and then divide it. If x/3 is not divisible by 3 and is greater than y, increment x until it becomes divisible by 3 and then divide it. Repeat this process until x becomes equal to y. Count the number of steps taken.

Code#

#include <iostream>
#include <vector>
using namespace std;
const int maxn = 70005;
typedef long long ll;
int T;
ll x, y;
int main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin >> T;
    while(T--) {
        cin >> x >> y;
        ll cnt = 0;
        while(x != y) { 
            while (x != y && x % 3 == 0 && x/3 >= y) {
                x /= 3;
                ++cnt;
            }
            if(x == y) break;

            if(x % 3 == 0) x /= 3,++cnt;
            if(x < y) { // Add to reach the target number
                cnt += y-x;
                break;
            } else {
                while(x != y && x % 3 != 0) {   // Add at most three times
                    ++x, ++cnt;
                }
            }
        }
        cout << cnt << endl;
    }
    return 0;
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.