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

Uber神仙操作电面

🔗
youhaoW 2018-11-30 04:00:39 来自APP | 只看该作者
全局:
考的是函数式编程基础。
第一次见面试问这个,没接触过概念的话是比较早短时间内接受。
回复

使用道具 举报

🔗
testcase 2018-11-30 05:41:24 | 只看该作者
全局:
有时面试官自己就是觉得不能让别人这么轻易就进来 然后出些奇奇怪怪的题
回复

使用道具 举报

全局:
想远眺的金蓝鸟 发表于 2018/11/30 03:09:03


唉,可以回邮件举报面试官吗?我真的想argue一下了

可以,不一定有用,看你的hr
回复

使用道具 举报

🔗
PeterR 2018-11-30 05:48:00 | 只看该作者
全局:
这是啥啊,函数式编程么???这考的出来个什么
回复

使用道具 举报

🔗
lantian_bihai 2018-11-30 05:51:08 | 只看该作者
全局:
。。。闻所未闻。。。。
真的奇葩
我以为系统设计上火星已经够可以的了
给Uber点赞
回复

使用道具 举报

🔗
karansh 2018-11-30 06:01:10 | 只看该作者
本楼:
全局:
谢谢分享
回复

使用道具 举报

🔗
nicezg 2018-11-30 06:03:29 | 只看该作者
全局:
U 家好像没有题库,面试官想怎么出就怎么出。
回复

使用道具 举报

🔗
magicsets 2018-11-30 06:52:54 | 只看该作者
全局:
这个题目出得很不严谨,因为认真考虑的话要处理的情况是非常多的(例如隐式类型转换、变长参数等等),面试小哥估计也是半桶水晃荡..

相关知识点本质上是与编译器前端中的function (overload) resolution有关,看起来楼主面试语言应该是Java,那么是要用Java reflection的一些技术

写了一份参考代码:
  1. import java.lang.reflect.Method;
  2. import java.util.HashMap;

  3. class Solution {
  4.   private static final HashMap<Class<?>, Class<?>> primitiveTypes =
  5.       new HashMap<Class<?>, Class<?>>() {{
  6.           put(boolean.class, Boolean.class);
  7.           put(byte.class, Byte.class);
  8.           put(char.class, Character.class);
  9.           put(double.class, Double.class);
  10.           put(float.class, Float.class);
  11.           put(int.class, Integer.class);
  12.           put(long.class, Long.class);
  13.           put(short.class, Short.class);
  14.           put(void.class, Void.class);
  15.       }};

  16.   public boolean testFunction(final Class<?> target,
  17.                               final Class<?> expected,
  18.                               final Object... args) {
  19.     // 实参类型
  20.     Class<?>[] actualTypes = new Class[args.length];
  21.     for (int i = 0; i < args.length; ++i) {
  22.       actualTypes[i] = args[i].getClass();
  23.     }

  24.     // 遍历所有method,匹配参数类型以及结果类型
  25.     for (Method method : target.getMethods()) {
  26.       if (checkEquivalence(method.getParameterTypes(), actualTypes) &&
  27.           checkEquivalence(method.getReturnType(), expected, false)) {
  28.         return true;
  29.       }
  30.     }
  31.     return false;
  32.   }

  33.   private boolean checkEquivalence(final Class<?>[] formalTypes,
  34.                                    final Class<?>[] actualTypes) {
  35.     // 如果要支持对变长参数(varargs)的检测,这里要写得更复杂一些
  36.     if (actualTypes.length != formalTypes.length) {
  37.       return false;
  38.     }
  39.     for (int i = 0; i < formalTypes.length; ++i) {
  40.       if (!checkEquivalence(formalTypes[i], actualTypes[i], true)) {
  41.         return false;
  42.       }
  43.     }
  44.     return true;
  45.   }

  46.   private boolean checkEquivalence(Class<?> formalType,
  47.                                    Class<?> actualType,
  48.                                    final boolean allowImplicitTypeCast) {
  49.     if (primitiveTypes.containsKey(formalType)) {
  50.       formalType = primitiveTypes.get(formalType);
  51.     }
  52.     // 这种简单写法可以支持隐式upcast,例如String -> Object
  53.     // 但是不支持隐式primitive类型转换,例如int -> float
  54.     // 如果要支持后者需要更大的代码量
  55.     return allowImplicitTypeCast ? formalType.isAssignableFrom(actualType)
  56.                                  : formalType.equals(actualType);
  57.   }
  58. }

  59. // 用于测试的class
  60. class TestCase {
  61.   public int f(int x, int y) {
  62.     return x + y;
  63.   }

  64.   public double g(String x, double y) {
  65.     return x.length() * y;
  66.   }

  67.   public String h(long id, String[] values) {
  68.     return "[" + id + "] " + String.join(" ", values);
  69.   }
  70. }

  71. public class Main {
  72.   public static void main(String[] args) {
  73.     Solution solution = new Solution();
  74.     // 测试 Solution.f
  75.     // true
  76.     System.out.println(solution.testFunction(TestCase.class, Integer.class, 1, 2));
  77.     // false, 参数数量不匹配
  78.     System.out.println(solution.testFunction(TestCase.class, Integer.class, 1, 2, 3));
  79.     // false, 第一个参数类型不匹配
  80.     System.out.println(solution.testFunction(TestCase.class, Integer.class, 1.5, 2));

  81.     // 测试 Solution.g
  82.     // true
  83.     System.out.println(solution.testFunction(TestCase.class, Double.class, "Hello", 1.5));
  84.     // false,返回值类型不匹配
  85.     System.out.println(solution.testFunction(TestCase.class, String.class, "Hello", 1.5));

  86.     // 测试 Solution.h
  87.     // true
  88.     System.out.println(solution.testFunction(TestCase.class, String.class, 1L, new String[] {"a", "b"}));
  89.     // false, 参数类型不匹配
  90.     System.out.println(solution.testFunction(TestCase.class, String.class, "Hello", new String[] {"a", "b"}));
  91.     // false, Solution里面的代码不支持隐式基础类型转换
  92.     System.out.println(solution.testFunction(TestCase.class, String.class, 1, new String[] {"a", "b"}));
  93.   }
  94. }
复制代码

评分

参与人数 1大米 +5 收起 理由
高渐离击筑高歌 + 5 很有用的信息!

查看全部评分

回复

使用道具 举报

🔗
 楼主| 想远眺的金蓝鸟 2018-11-30 07:02:58 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
 楼主| 想远眺的金蓝鸟 2018-11-30 07:03:47 | 只看该作者
全局:
PeterR 发表于 2018-11-30 05:48
这是啥啊,函数式编程么???这考的出来个什么

好像是用Method class进行参数格式匹配~
回复

使用道具 举报

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

本版积分规则

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