ADP Lifion开发三轮,考数据库和算法

【算法】

题目:
input string= "appleeeep"
input character= "p"
输出每个字母到达最近的p的距离,比如上面对应的输出为:
output= {1,0,0,1,2}

面试的过程:

因为实在是太简单了……我先是用文字说了思路、复杂度,问他就这样还是需要优化。我觉得这很好,不过前提是真的会做这道题。

我出了一点小bug,不用加if (i != j),应该是添加距离Math.abs(j - i)而不是index j。

然后面试官要带入test case去跑

input string= "appleeeep"
input character= "p"
output= {1,0,0,1,2}

//
loop thru the string, 
for each char, for loop chars from the beginning
  if the  is 'p', put the 'p's indexs into an list
  get the smallest value in the list eg[6,5,1] --min
  calculate the distance, 
  --nearest, break, put into the output

time:n^2, space: n

*/

class Solution {
  public static void main(String[] args) {
    //for loop thru the string
    for (int i = 0; i < string.length(); i++) {
      //for loop thru the string and find the 'char's 
      for (int j = 0; j < string.length(); j++) {
        ArrayList<Integer> indexs = new ArrayList<Integer>();
        //if (i != j) {
          //judge, if == char, add to an arraylist
          if (string.charAt(j) == char) {
            //add distance
            indexs.add(MAth.abs(j - i));  ---- here
          }
        }
      }
      //after added all qualified j, compare and find the minimum j
      int min = Integer.MAX_VALUE;
      for (int k = 0; k < indexs.size(); k++) {
          min = Math.main(min, indexs.get(k));
      }
      output.add(min);
    }
    
    return output;
  }
}

// input string= "appleeeep"
// input character= "p", char
// output= {1,0,0,1,2}
loop from a,
loop from beginning indexs =[1,2,9] -->1, add to output --[1,..]

loop from e,
loop from beginning indexs = [3,2,4] -->2,  add to output [2]
我当时写的草稿

【数据库设计】
Can you develop an RDBMS data model for a forum web application, where the basic functionality is:
1.  Users can sign up
2. They can post topics within a topic category
3. They can reply to post
4. They can vote on each other’s posts.

我设计的表格:

User(user_id,user_name..)
topic(topic_id,topic_category,topic_id_specific)
Post_Reply(Post_id, topic_id@, topic_id_specific@, user_id@, user_id@, layer_id) //first make the post, someone who reply
Post_Vote(Post_id, topic_id@, topic_id_specific@, user_id@)

后面新加了需求:
a.每个topic下面有子topic怎么设计,比如health下面可以分为running, tomatoes。

我设计的结构是加个字段:topic_id_specific,也不知道对不对(捂脸……)

User
1, Mark
2, Yue

Topic
1, Health, Running
2, Health, Tomatoes


Post Reply
1, 1, Running, 1, null, I like to run, 1/1/2021, 1
2, 1, Running, 1, 2, Me too, 1/2/2021, 2
5, 1, Running, 1, 1, I like to run in the mountains, 1

b.如果有嵌套回复怎么办,类似于这种结构:
I like to run
----Me too
--------Me three
----I like to run in the mountains
--------I think lakes are better to run around

于是我又加了个字段:layer_id,也不知道对不对(捂脸……)

Post Reply
1, 1, Running, 1, null, I like to run, 1/1/2021, 1
2, 1, Running, 1, 2, Me too, 1/2/2021, 2
5, 1, Running, 1, 1, I like to run in the mountains, 1


I like to run 1st
----Me too 2nd
--------Me three 3rd
----I like to run in the mountains
--------I think lakes are better to run around
Can you develop a RDBMS data model for a forum web application, where the basic functionality is:
    1. Users can sign up
    2. They can post topics within a topic category
    3. They can reply to post
    4. They can vote on each other’s posts.

Entity1: Field1, Field2, Field3

User(user_id,user_name..)
topic(topic_id,topic_category,topic_id_specific)
Post_Reply(Post_id, topic_id@, topic_id_specific@, user_id@, user_id@, layer_id) //first make the post, someone who reply
Post_Vote(Post_id, topic_id@, topic_id_specific@, user_id@)

User
1, Mark
2, Yue

Topic
1, Health, Running
2, Health, Tomatoes


Post Reply
1, 1, Running, 1, null, I like to run, 1/1/2021, 1
2, 1, Running, 1, 2, Me too, 1/2/2021, 2
5, 1, Running, 1, 1, I like to run in the mountains, 1


I like to run 1st
----Me too 2nd
--------Me three 3rd
----I like to run in the mountains
--------I think lakes are better to run around


3, 2, Tomatoes, 1, null, Tomatoes are great for you, 2/1/2021
4, 2, Tomatoes, 1, 2, No they arent, 2/2/2021

topic_id@ more specific, 1  + add a field (Running/Tomatoes)
整个的草稿
原文地址:https://www.cnblogs.com/immiao0319/p/14439149.html