特斯拉pre-test (Go)

特斯拉pre-test (Go)

  • [1 Q1](#1 Q1)
  • [2 Q2](#2 Q2)
  • [3 Q3](#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].


翻译:

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

截图:

代码:

go 复制代码
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.

  1. 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).

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.


翻译:

bash 复制代码
给出了由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长度相等;
                每条道路连接两个不同的城市。
                两个城市最多只有一条直达道路。

截图:

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];

翻译:

bash 复制代码
编写一个函数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]范围内的整数;

截图:

相关推荐
TANGLONG2223 小时前
【初阶数据结构与算法】八大排序算法之归并排序与非比较排序(计数排序)
java·数据结构·c++·算法·面试·蓝桥杯·排序算法
独行soc3 小时前
#渗透测试#漏洞利用#红蓝攻防#信息泄露漏洞#Tomcat信息泄露漏洞的利用
安全·web安全·面试·tomcat·漏洞挖掘·信息泄露·tomcat漏洞
正在绘制中4 小时前
Java重要面试名词整理(六):MongoDB
java·mongodb·面试
Pandaconda4 小时前
【Golang 面试题】每日 3 题(六)
开发语言·笔记·后端·面试·职场和发展·golang·go
峰子20126 小时前
Go语言实现守护进程的挑战
开发语言·后端·面试·架构·golang·go
YRr YRr7 小时前
全国硕士研究生入学考试(考研)常识详解之复试考试科目:笔试、面试与加试
考研·面试·职场和发展
绝无仅有7 小时前
gozero项目日志Prometheus的配置与实战
后端·面试·架构
HEU_firejef8 小时前
面试经典 150 题——数组/字符串(一)
数据结构·算法·面试
码农爱java8 小时前
设计模式--装饰器模式【结构型模式】
java·设计模式·面试·装饰器模式·原理·23 中设计模式
绝无仅有8 小时前
gozero项目日志收集与配置实战
后端·面试·架构