特斯拉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 Ai and Bi 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 AP + AP+1 + ... + AQ 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]范围内的整数;

截图:

相关推荐
Yeyu1 小时前
Android 渲染流水线全解析:从 Choreographer 到 SurfaceFlinger
面试
Java编程爱好者3 小时前
OpenEvent:事件驱动、日志先行的Agent框架
面试
destinying3 小时前
前端秒变AI全栈,我的核心资产是一套Node.js“中间件”
前端·后端·面试
AI人工智能+电脑小能手4 小时前
【大白话说Java面试题 第91题】【Mysql篇】第21题:分布式锁的使用场景和原理?
java·数据库·分布式·mysql·面试
JAVA社区4 小时前
Java高级全套教程(十三)—— 分布式锁超详细实战详解(原理+三种方案企业级落地)
java·开发语言·分布式·spring cloud·面试·java-zookeeper
Mahir084 小时前
MyBatis 延迟加载深度解密:从使用方式到底层动态代理原理全解
java·后端·面试·mybatis
贺国亚5 小时前
Multi-Agent 与 Multi-Task 编排架构
面试
神奇小汤圆5 小时前
滴滴面试官摇头:"你 SKILL.md 全塞进 context 了?我刚翻完 Anthropic 文档,人家是按需加载的。" 我后背一凉
面试
仙俊红7 小时前
线程池面试
python·面试·职场和发展
小江的记录本8 小时前
【JVM虚拟机】类加载机制:类加载器、双亲委派模型、好处、破坏双亲委派的场景(附《思维导图》+《面试高频考点清单》)
java·jvm·spring boot·后端·python·spring·面试