特斯拉pre-test (Go)

特斯拉pre-test (Go)

  • 1 Q1
  • 2 Q2
  • 3 Q3

1 Q1

原文:
You are given an implementation of a function Solution that, given a positive integer N, prints to standard output another integer, which was formed by reversing a decimal representation of N. The leading zeros of the resulting integer should not be printed by the function.
Examples:

  1. Given N = 54321, the function should print 12345.
  2. Given N = 10011, the function should print 11001.
  3. Given N = 1, the function should print 1.

The attached code is still incorrect for some inputs. Despite the error(s), the code may produce a correct answer for the example test cases. The goal of the exercise is to find and fix the bug(s) in the implementation. You can modify at most three lines.

Assume that:
N is an integer within the range [1…1,000,000,000].


翻译:

给你一个函数解决方案的实现,给定一个正整数N,将另一个整数打印到标准输出,该整数是通过反转N的十进制表示形式形成的。结果整数的前导零不应由函数打印。
示例:
	1.给定N=54321,该函数应打印12345。
	2.给定N=10011,函数应该打印11001。
	3.给定N=1,函数应该打印1。
附加的代码对于某些输入仍然不正确。尽管有错误,代码可能会为示例测试用例产生正确的答案。练习的目标是找到并修复实现中的bug。您最多可以修改三行。
假设:
	N是[1…1,000,000,000]范围内的整数。

截图:
特斯拉pre-test (Go)_第1张图片
代码:

package main

import "fmt"

func Solution(N int) {
	var enable_print int
	enable_print = N % 10
	for N > 0 {
		if enable_print == 0 && N%10 != 0 {
			enable_print = 1
		} else if enable_print == 1 {
			fmt.Print(N % 10)
		}
		N = N / 10
	}
}

2 Q2

原文:
An infrastructure consisting of N cities, numbered from 1 to N, and M bidirectional roads between them is given. Roads do not intersect apart from at their start and end points (they can pass through underground tunnels to avoid collisions).
For each pair of cities directly connected by a road, let’s define their network rank as the total number of roads that are connected to either of the two cities.
Write a function:
func Solution(A []int, B []int, N int) int
that, given two arrays A, B consisting of M integers each and an integer N, where A[i] and B[i] are cities at the two ends of the i-th road, returns the maximal network rank in the whole infrastructure.
Examples:
1. Given A = [1, 2, 3, 3], B = [2, 3, 1, 4] and N = 4, the function should return 4. The chosen cities may be 2 and 3, and the four roads connected to them are: (2, 1), (2, 3), (3, 1), (3, 4).
In the pictures below, the chosen cities and roads connected to them are marked in red.
特斯拉pre-test (Go)_第2张图片
2. Given A = [1, 2, 4, 5], B = [2, 3, 5, 6] and N = 6, the function should return 2. The chosen cities may be 1 and 2, and the two roads connected to them are: (1, 2), (2, 3).
特斯拉pre-test (Go)_第3张图片
Write an efficient algorithm for the following assumptions:
N is an integer within the range [2…100];
M is an integer within the range [1…4,950];
each element of arrays A and B is an integer within the range [1…N];
A and B have equal length;
each road connects two distinct cities;
two cities are connected by at most one direct road.


翻译:

给出了由N个城市组成的基础设施,编号从1到N,它们之间有M条双向道路。道路不会在起点和终点相交(它们可以通过地下隧道以避免碰撞)。
对于每一对由道路直接连接的城市,让我们将它们的网络排名定义为连接到两个城市之一的道路总数。
写一个函数:
        func解决方案(A[]int, B[]int,N int)int
给定由M个整数和一个整数N组成的两个数组A、B,其中A[i]和B[i]是第i条路两端的城市,返回整个基础设施中的最大网络排名。
示例:
        1.给定A=[1,2,3,3],B=[2,3,1,4],N=4,函数应返回4,选择的城市可能是2和3,与它们相连的四条道路是:(2,1),(2,3),(3,1),(3,4)。
        在下面的图片中,选择的城市和与之相连的道路用红色标记。

        2.给定A=[1,2,4,5],B=[2,3,5,6],N=6,函数应返回2,选择的城市可能是1和2,与它们相连的两条道路是:(1,2),(2,3)。

        为以下假设编写一个有效的算法:
                N是范围[2…100]内的整数;
                M是[1…4,950]范围内的整数;
                数组A和B的每个元素都是[1… N]范围内的整数;
                A和B长度相等;
                每条道路连接两个不同的城市。
                两个城市最多只有一条直达道路。

截图:
特斯拉pre-test (Go)_第4张图片
特斯拉pre-test (Go)_第5张图片

3 Q3

原文:
Write a function Solution that, given an array A consisting of N integers, returns the number of fragments of A whose sum equals 0 (that is, pairs (P, Q) such that P ≤ Q and the sum A[P] + A[P+1] + … + A[Q] is 0). The function should return −1 if this number exceeds 1,000,000,000.
Examples:

  1. Given A = [2, −2, 3, 0, 4, −7], the function should return 4, as explained on this picture:
    在这里插入图片描述
  2. Given A = [0, 0, …, 0] of length 100,000, the function should return −1.
    Write an efficient algorithm for the following assumptions:
    • N is an integer within the range [1…100,000];
    • each element of array A is an integer within the range [−10,000…10,000];

翻译:

编写一个函数Solution,给定一个由N个整数组成的数组A,返回A的总和等于0的片段数(即,对(P, Q)使得P≤Q并且总和A[P]+A[P+1]+…+A[Q]为0)。如果此数字超过1,000,000,000,则该函数应返回-1。
示例:
1.给定A=[2,−2,3,0,4,−7],函数应该返回4,如下图所示:
2.给定长度为100,000的A=[0,0,…,0],该函数应返回-1。
为以下假设编写一个有效的算法:
 N是范围[1…100,000]内的整数;
 数组A的每个元素都是[−10,000…10,000]范围内的整数;

截图:
特斯拉pre-test (Go)_第6张图片

你可能感兴趣的:(面试,面试)