Interview questions that max out the circle of friends? Byte beating coding problem analysis ideas sharing!

Foreword:

Near the National Day, my friends in a small circle of WeChat group posted a picture of the byte-beating interview question code circulated on the Internet. I thought about it when I had nothing to do. It was not difficult to find, it was all about the basics. Consideration of mathematics knowledge. Take the picture first!

Of course, I can't code any two questions in 40 minutes. I just know the general idea of ​​solving the problem. The only thing I can be sure of is that I can code the second question within 20 minutes within the specified time of the interview.

Insert picture description here

Theme

Topic One

Insert picture description here

The basic knowledge is the plane Cartesian coordinate system of junior high school, analytical ideas:

  • Calculate the total circumference;
  • Calculate and encapsulate the front and back coordinates of each side length, and use it in the fourth step;
  • Calculate the average segment length according to the K segment value;
  • Then loop K times, and calculate the coordinates of the bisecting points according to the average length.

Not much to say, the code:

First define the Point class of coordinates

class Point {
    
    
        float x;
        float y;
 
        public Point() {
    
    
        }
 
        public Point(float x, float y) {
    
    
            this.x = x;
            this.y = y;
        }
 
        public Point(Point point) {
    
    
            this(point.x, point.y);
        }
 
        @Override
        public String toString() {
    
    
            return "Point, x:" + x + " y:" + y;
        }
    }
N边形的边封装类


class Line {
    
    
        Point begin;
        Point end;
        float length;
 
        public Line() {
    
    
 
        }
 
        public Line(Point begin, Point end, float length) {
    
    
            this.begin = begin;
            this.end = end;
            this.length = length;
        }
    }

Classes that implement calculations now

In the first version of this code, when the square was divided into even numbers, the coordinate point calculation was not accurate. Tonight, I looked at the code for 10 minutes and changed it slightly. There is no such bug temporarily. We look forward to finding out other bugs and fixing them!

public class Polygon {
    
    
 
    /**
     * 计算边的长度
     * 
     * @return
     */
    private static float lineLength(Point a, Point b) {
    
    
        float length;
 
        if (a.x == b.x) {
    
    
            // 垂直线条
            length = Math.abs(a.y - b.y);
        } else {
    
    
            length = Math.abs(a.x - b.x);
        }
 
        return length;
    }
 
    /**
     * 计算 周长
     * 
     * @return
     */
    private static float totalSideLength(Point[] points, Line[] lines) {
    
    
        float side = 0;
 
        for (int i = 1; i < points.length; i++) {
    
    
            Point prev = points[i - 1];
            Point point = points[i];
 
            float length = lineLength(prev, point);
 
            side += length;
            lines[i - 1] = new Line(prev, point, length);
 
            if (i == points.length - 1) {
    
    
                length = lineLength(point, points[0]);
 
                side += length;
                lines[i] = new Line(point, points[0], length);
            }
        }
 
        return side;
    }
 
    public static Point[] division(Point[] points, int divisionNum) {
    
    
        Point[] divisionPoint = new Point[divisionNum];
 
        // 计算周长
        Line[] lines = new Line[points.length];
        float side = totalSideLength(points, lines);
 
        // 等分长度
        float divisionLength = side / divisionNum;
 
        int lineIndex = -1;
        float sumLength = 0;
 
        for (int i = 0; i < divisionNum; i++) {
    
    
            if (i == 0) {
    
    
                // 第一个等分点直接是起始点坐标
                divisionPoint[i] = new Point(points[0]);
                continue;
            }
 
            divisionPoint[i] = new Point();
            float lineLength = divisionLength * i;
 
            while (true) {
    
    
                Line line;
                if (sumLength < lineLength) {
    
    
                    lineIndex++;
                    line = lines[lineIndex];
                    sumLength += line.length;
                } else
                    line = lines[lineIndex];
 
                if (sumLength >= lineLength) {
    
    
                    float temp = sumLength - lineLength;
 
                    if (line.begin.x == line.end.x) {
    
    
                        // begin和end的坐标点垂直
                        divisionPoint[i].x = line.begin.x;
 
                        if (line.end.y > line.begin.y)
                            divisionPoint[i].y = line.end.y - temp;
                        else
                            divisionPoint[i].y = line.end.y + temp;
                    } else {
    
    
                        // begin和end的坐标点水平
                        divisionPoint[i].y = line.end.y;
 
                        if (line.end.x > line.begin.x)
                            divisionPoint[i].x = line.end.x - temp;
                        else
                            divisionPoint[i].x = line.end.x + temp;
                    }
                     
                    break;
                }
            }
        }
 
        return divisionPoint;
    }
 
    private static void print(Point[] points) {
    
    
        for (int i = 0; i < points.length; i++) {
    
    
            System.out.println("第" + (i + 1) + "等分点, x:" + points[i].x + ",y:" + points[i].y);
        }
    }
 
    public static void main(String[] args) {
    
    
        Point[] points = new Point[] {
    
     new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 0) };
 
        Point[] divPoints = division(points, 8);
 
        print(divPoints);
    }
}

Topic two

Insert picture description here

Problem-solving ideas:

The sum of the corresponding digits will never exceed 18. Therefore, we first calculate the sum of the corresponding positions, and then repeatedly loop to find the number greater than 9, and carry to the higher digit.

This is relatively simple, just examine the detail that the addition of single-digit positive integers is never greater than 18.

On the code:

public class LinkAddition {
    
    
    static class NumNode {
    
    
        public int num;
        public NumNode next;
 
        public NumNode() {
    
    
        }
 
        public NumNode(int num) {
    
    
            this.num = num;
        };
 
        public NumNode(int num, NumNode next) {
    
    
            this(num);
            this.next = next;
        }
    }
 
    private static int length(NumNode num) {
    
    
        int length = 0;
 
        NumNode temp = num;
        while (temp != null) {
    
    
            length++;
            temp = temp.next;
        }
 
        return length;
    }
 
    private static NumNode calc(NumNode a, NumNode b, int aLength, int bLength) {
    
    
        NumNode aNode = a;
        NumNode bNode = b;
 
        NumNode result = new NumNode();
        NumNode resultNode = result;
 
        // 计算b链表再a中的起始索引
        int aStartIndex = aLength - bLength;
 
        for (int i = 0; i < aLength; i++) {
    
    
            if (i >= aStartIndex) {
    
    
                resultNode.num = aNode.num + bNode.num;
                bNode = bNode.next;
            } else
                resultNode.num = aNode.num;
 
            aNode = aNode.next;
            if (aNode != null) {
    
    
                resultNode.next = new NumNode();
                resultNode = resultNode.next;
            }
        }
 
        return result;
    }
 
    public static NumNode addition(NumNode a, NumNode b) {
    
    
        NumNode result = null;
 
        // 计算位数
        int aLength = length(a);
        int bLength = length(b);
 
        if (aLength > bLength) {
    
    
            result = calc(a, b, aLength, bLength);
        } else {
    
    
            result = calc(b, a, bLength, aLength);
        }
 
        boolean isGreater9 = true;
 
        while (isGreater9) {
    
    
            isGreater9 = false;
            NumNode node = result;
 
            while (node != null) {
    
    
                // 检查是否有大于9的节点
                if (node.num > 9) {
    
    
                    isGreater9 = true;
                    break;
                }
 
                node = node.next;
            }
 
            // 没有大于9且需要进位的节点
            if (!isGreater9)
                break;
             
            node = result;
             
            if (node.num > 9) {
    
    
                // 头节点的内容跟大于9,需要进位
                result = new NumNode(1, node);
 
                node.num = node.num - 10;
            }
 
            while (node.next != null) {
    
    
                if (node.next.num > 9) {
    
    
                    node.num += 1;
                    node.next.num = node.next.num - 10;
                }
                node = node.next;
            }
        }
 
        return result;
    }
 
    private static void print(NumNode num) {
    
    
        NumNode node = num;
        while (node != null) {
    
    
            System.out.print(node.num);
            node = node.next;
        }
    }
 
    public static void main(String[] args) {
    
    
        NumNode a = new NumNode(9);
        a.next = new NumNode(9, new NumNode(9));
 
        NumNode b = new NumNode(9);
        // b.next = new NumNode(9, new NumNode(9));
 
        NumNode result = addition(a, b);
 
        print(result);
    }
}

Topic Three

Insert picture description here

The first version I wrote only fits the example of the class, and then I overturned the class in an instant. Finally, I sat down and thought about the class for 10 minutes, and parsed it as a two-dimensional array.

Find the highest point first, and then use the highest point as a dimension to calculate the amount of water in a loop. Let's add the code:

public class Water {
    
    
    public static int waterNum(int[] steps) {
    
    
        int waterNum = 0;
 
        int max = steps[0];
        for (int i = 1; i < steps.length; i++) {
    
    
            if (max < steps[i])
                max = steps[i];
        }
 
        for (int i = 0; i < max; i++) {
    
    
            int num = 0, index = 0;
 
            for (int n = 0; n < steps.length; n++) {
    
    
                if (steps[n] - i > 0) {
    
    
                    if (num > 0) {
    
    
                        waterNum += n - index - 1;
                    }
 
                    num = steps[n] - i;
                    index = n;
                }
            }
        }
 
        return waterNum;
    }
 
    public static void main(String[] args) {
    
    
        int[] steps = new int[] {
    
     0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 3, 0, 1 };
        int water = waterNum(steps);
 
        System.out.println(water);
    }
}

to sum up:

In fact, the knowledge points of these questions are not difficult, they are all used in ordinary times, it depends on how to convert them into code.

The first question examines how to calculate the side length in the rectangular coordinate system, and then walk from the first side to calculate the corresponding coordinates according to the equal length. This knowledge point has been learned in junior high school.

The second question is to investigate the maximum number of positive integer additions on each bit. As long as you understand this point, after adding each bit, you can do a unified carry process.

The amount of code for the third question is the least. My idea of ​​solving the problem is a two-digit array, which is not too difficult.
Recently, it is the best time to find a job. I have collected some interview questions from major manufacturers and the latest data this year (2020). The following are some screenshots of the data (all data have been integrated into documents, and pdf compressed and packaged) .
If you have a friend in need, you can click here to get information, code: qf
Insert picture description here

Guess you like

Origin blog.csdn.net/SpringBoot_/article/details/108740322
Recommended