回复: 7
跳转到指定楼层
上一主题 下一主题
收起左侧

新鲜G家店面

全局:

2019(1-3月) 码农类General 硕士 全职@google - 猎头 - 技术电面  | | Other | 在职跳槽

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
题目是 排名选举系统
刚刚面了狗家店面,题目是这样的:
假如,有4个可以选的candidate [红,黄,蓝,绿]。顺序[红,黄,蓝,绿] 代表 红》黄》蓝》绿。 每个选民提交自己的选票顺序,比如,选民a可以提交【绿,蓝,红,黄】,选民b可以提交【红,绿,蓝,黄】等等。写出下面算法完成选举:
第一
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
觉得这个题不太好做phone interview的candidate, 做onsite还好。面试过程中,我们说半天都说不清楚。面试官一会说这样,一会儿又推翻之前说的,我真的是很懵逼。。。大家有没有碰到类似说不清的情况?

评分

参与人数 2大米 +22 收起 理由
匿名用户-PADNZ + 20
mhsasd + 2 给你点个赞!

查看全部评分


上一篇:🐶🐶 昂塞特
下一篇:Factual SDE OA
🔗
林微熙 2019-4-9 03:37:23 | 只看该作者
本楼:
全局:
LC 911变形???????
回复

使用道具 举报

🔗
 楼主| mojolady 2019-4-9 03:56:02 | 只看该作者
全局:

感觉比911复杂多了。。。
回复

使用道具 举报

全局:
选票占比怎么算的?
回复

使用道具 举报

🔗
 楼主| mojolady 2019-4-10 03:22:08 | 只看该作者
全局:
mhsasd 发表于 2019-4-9 05:28
选票占比怎么算的?

出题人只说了上面那些,他说怎么算出选票分布要自己想。所以我就给了一个weight在不同的position上,然后计算的,不过也不知道对不对。
回复

使用道具 举报

🔗
 楼主| mojolady 2019-4-19 08:30:27 | 只看该作者
全局:
直接给大家粘一卡他要的代码。。。。
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Iterator;
  4. import java.util.Scanner;
  5. import java.util.Set;
  6. import java.util.Map;
  7. import java.util.TreeSet;
  8. import java.util.TreeMap;

  9. /**
  10. * Runs an instant runoff election.
  11. * In an instant runoff election each voter submits a list of candidates,
  12. * in order of preference.  The first name on the list is the voter's
  13. * first choice, the second the voter's second choice, etc.  The voter
  14. * need not list all of the candidates.
  15. *
  16. * The election is conducted in rounds.  Each voter's first choice is
  17. * tallied, and the candidate with the fewest first-place votes is
  18. * eliminated.  (If there is a tie one of the lowest vote-getters is chosen
  19. * at random to be eliminated.)  In each round the voter's top choice
  20. * amongst the remaining candidates gets that voter's vote for that round.
  21. * If no current candidate is on a voter's list that voter casts no vote
  22. * for this round.
  23. *
  24. * The process ends when there is a single candidate left, who is declared
  25. * the winner.
  26. *
  27. * This version is written in a procedural style, with lots of static methods.
  28. *
  29. * @author Scot Drysdale
  30. */
  31. public class InstantRunoffProc {
  32.        
  33.         private static boolean debugOn = true;    // Print debugging output?
  34.        

  35.         /**
  36.          * Creates a set of candidate names.  A candidate is someone who appears
  37.          * on at least one ballot.
  38.          * @param ballots the set of ballots
  39.          * @return the set of candidate names
  40.          */
  41.         public static Set<String> getInitialCandidates(List<List<String>> ballots) {
  42.                 Set<String> candidateSet = new TreeSet<String>();
  43.                
  44.                 // Add all names on all ballots to the set.  Adding a candidate who
  45.                 // is already in the set does not change the set.
  46.                 for(List<String> ballot : ballots)
  47.                         if(ballot.size() > 0)
  48.                                 candidateSet.add(ballot.get(0));  
  49.                
  50.                 return candidateSet;
  51.         }
  52.        
  53.         /**
  54.          * Counts the number of votes that each candidate gets.  Only current
  55.          * candidates can receive votes.
  56.          * @param candidates the current list of candidates
  57.          * @param ballots a set of lists of voter preferences
  58.          * @return the vote tally for each candidate
  59.          */
  60.         public static Map<String, Integer> countVotes(Set<String> candidates,
  61.                         List<List<String>> ballots) {
  62.                 Map<String, Integer> voteTally = new TreeMap<String, Integer>();
  63.                
  64.                 // Set up an entry for each current candidate with no votes.
  65.                 for(String candidate : candidates)
  66.                         voteTally.put(candidate, 0);
  67.           
  68.                 // Tally up top choice on each ballot
  69.                 for(List<String> ballot : ballots) {
  70.                         String topChoice = getTopChoice(ballot, candidates);
  71.                         if(topChoice != null) {
  72.                                 int currentTally = voteTally.get(topChoice);
  73.                                 voteTally.put(topChoice,  currentTally + 1);
  74.                         }
  75.                 }
  76.                 return voteTally;       
  77.         }
  78.        
  79.         /**
  80.          * Finds and returns the top choice among the remaining candidates.
  81.          * @param ballot a voter's ballot (preferential list of candidates).
  82.          * @param candidates the current set of candidates
  83.          * @return name of top choice in candidates.
  84.          */
  85.         public static String getTopChoice(List<String> ballot, Set<String> candidates) {
  86.                 Iterator<String> iter = ballot.iterator();
  87.                 while(iter.hasNext()) {
  88.                         String candidate = iter.next();
  89.                         if(candidates.contains(candidate))
  90.                                 return candidate;
  91.                 }
  92.                 return null;  // None of the candidates on ballot were in candidates
  93.         }
  94.        
  95.         /**
  96.          * Finds the candidates with the fewest votes.
  97.          * @param candidates the current list of candidates
  98.          * @param voteTally a map from candidates to votes
  99.          * @return a list of candidates with the fewest votes.
  100.          */
  101.         public static ArrayList<String> getLosers(Set<String> candidates,
  102.                         Map<String, Integer> voteTally) {
  103.                 ArrayList<String> loserList = new ArrayList<String>();
  104.                 int minTally = Integer.MAX_VALUE;  // Bigger than any possible vote count
  105.                
  106.                 for(String candidate : voteTally.keySet()) {
  107.                         int candidateTally = voteTally.get(candidate);
  108.                         if(candidateTally < minTally) {  // Found new loser?
  109.                                 loserList.clear();   
  110.                                 loserList.add(candidate);      // Remember new loser
  111.                                 minTally = candidateTally;                       
  112.                         }
  113.                         else if(candidateTally == minTally)
  114.                                 loserList.add(candidate);      // Have another with the same low tally
  115.                 }
  116.                 return loserList;
  117.         }
  118.        
  119.         /**
  120.          * Picks random item from a list
  121.          * @list the list to choose from
  122.          * @return an item chosen randomly from list
  123.          */
  124.         public static String pickRandomItem(ArrayList<String> list) {
  125.                 return list.get((int) (Math.random()*list.size()));
  126.         }
  127.        
  128.         /**
  129.          * Runs an instant-runoff election
  130.          * @param ballots the ballots for this election
  131.          * @return winner of the election
  132.          */
  133.         public static String runInstantRunoffElection(List<List<String>> ballots) {
  134.                 Set<String> candidates = getInitialCandidates(ballots);

  135.                
  136.                 // Run rounds until down to a single candidate
  137.                 while(candidates.size() > 1) {
  138.                         Map<String, Integer> voteTally = countVotes(candidates, ballots);
  139.                         String loser = pickRandomItem(getLosers(candidates, voteTally));
  140.                         candidates.remove(loser);
  141.                        
  142.                         if(debugOn) {
  143.                                 System.out.println("Vote tally:\n" + voteTally);
  144.                                 System.out.println("Loser: " + loser);
  145.                         }
  146.                 }
  147.                
  148.                 if(candidates.size() > 0)
  149.                   return candidates.iterator().next(); // Return the surviving candidate
  150.                 else
  151.                   return null;
  152.         }

  153.         /**
  154.          * Test program
  155.          */
  156.         public static void main(String[] args) {
  157.                 List<List<String>> ballots = new ArrayList<List<String>>();
  158.                
  159.                 System.out.println("Enter candidate names in order of preference,");
  160.                 System.out.println("separated by spaces.  End with a blank line.");
  161.                 List<String> ballot;
  162.                 Scanner in = new Scanner(System.in);
  163.                
  164.                 System.out.print("Enter a ballot: ");
  165.                 String line = in.nextLine();
  166.                 while(!line.equals("")) {
  167.                         ballot = new ArrayList<String>();
  168.                         Scanner inLine = new Scanner(line);
  169.                        
  170.                         while(inLine.hasNext()) {
  171.                                 String candidate = inLine.next();
  172.                                 ballot.add(candidate);
  173.                         }
  174.                         ballots.add(ballot);
  175.                        
  176.                         System.out.print("Enter a ballot: ");
  177.                         line = in.nextLine();
  178.                 }
  179.                
  180.                 String winner = runInstantRunoffElection(ballots);
  181.                 if (winner != null)
  182.             System.out.println("The winner of the instant runoff election is: " +
  183.                     winner);
  184.                 else
  185.                         System.out.println("No valid votes cast");
  186.         }
  187. }
复制代码
回复

使用道具 举报

🔗
大头菜 2019-4-23 05:20:28 | 只看该作者
全局:
这个电面好狠啊 楼主有消息了吗
回复

使用道具 举报

🔗
 楼主| mojolady 2019-4-23 06:31:30 | 只看该作者
全局:
大头菜 发表于 2019-4-23 05:20
这个电面好狠啊 楼主有消息了吗

据掉了。。。哭😢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表