Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
1,216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. Test assignment
  2.  
  3. Thank you for participating in our recruiting test. This will be a C++ programming test!
  4. How to prepare for this test
  5.  
  6. Install an editor and C++ compiler of your choice. Your solution will be compiled with GCC 7.2.0 (with -std=c++17), and you will have the chance to fix any compilation errors prior to submission. You may use the standard libraries for the task. It is not permitted to use other libraries, however you will not need them anyway.
  7.  
  8. You are free in your choice of operating system and development environment. The task is a very general programming assignment testing general problem structuring and programming proficiency. The solution has to be submitted within a 9 hour time frame. Excellent C++ specialists will of course solve the problem in 3 to 4 hours, and we leave it to the candidates to test their solutions thoroughly before handing them in (in case they finish early).
  9.  
  10. You must develop the solution yourself. You may not let others help you or search for existing solutions. Of course you may use any documentation of the C++ language or the C++ Standard Library. Do not give your solution to others or make it public. It will entice others into sending in plagiarized solutions. If you use an online compiler, make sure that the privacy settings are set to private. Publishing the task description is also considered cheating and will void your application.
  11.  
  12. The amount of code you need for a correct solution is relatively small, and you will spend most of the allocated time thinking rather than typing. We would like to receive the best solution you can come up with!
  13. Task Description
  14.  
  15. interval_map<K,V> is a data structure that efficiently associates intervals of keys of type K with values of type V. Your task is to implement the assign member function of this data structure, which is outlined below.
  16.  
  17. interval_map<K, V> is implemented on top of std::map. In case you are not entirely sure which functions std::map provides, what they do and which guarantees they provide, we provide an excerpt of the C++ standard here:
  18.  
  19. Each key-value-pair (k,v) in the std::map means that the value v is associated with the interval from k (including) to the next key (excluding) in the std::map.
  20.  
  21. Example: the std::map (0,'A'), (3,'B'), (5,'A') represents the mapping
  22.  
  23. 0 -> 'A'
  24. 1 -> 'A'
  25. 2 -> 'A'
  26. 3 -> 'B'
  27. 4 -> 'B'
  28. 5 -> 'A'
  29. 6 -> 'A'
  30. 7 -> 'A'
  31. ... all the way to numeric_limits<int>::max()
  32.  
  33. The representation in the std::map must be canonical, that is, consecutive map entries must not have the same value: ..., (0,'A'), (3,'A'), ... is not allowed. Initially, the whole range of K is associated with a given initial value, passed to the constructor of the interval_map<K,V> data structure.
  34.  
  35. Key type K
  36.  
  37. besides being copyable and assignable, is less-than comparable via operator<
  38. is bounded below, with the lowest value being std::numeric_limits<K>::lowest()
  39. does not implement any other operations, in particular no equality comparison or arithmetic operators
  40.  
  41. Value type V
  42.  
  43. besides being copyable and assignable, is equality-comparable via operator==
  44. does not implement any other operations
  45.  
  46. You are given the following source code:
  47.  
  48. #include <map>
  49. #include <limits>
  50.  
  51. template<typename K, typename V>
  52. class interval_map {
  53. std::map<K,V> m_map;
  54.  
  55. public:
  56. // constructor associates whole range of K with val by inserting (K_min, val)
  57. // into the map
  58. interval_map( V const& val) {
  59. m_map.insert(m_map.end(),std::make_pair(std::numeric_limits<K>::lowest(),val));
  60. }
  61.  
  62. // Assign value val to interval [keyBegin, keyEnd).
  63. // Overwrite previous values in this interval.
  64. // Conforming to the C++ Standard Library conventions, the interval
  65. // includes keyBegin, but excludes keyEnd.
  66. // If !( keyBegin < keyEnd ), this designates an empty interval,
  67. // and assign must do nothing.
  68. void assign( K const& keyBegin, K const& keyEnd, V const& val ) {
  69.  
  70.  
  71. }
  72.  
  73. // look-up of the value associated with key
  74. V const& operator[]( K const& key ) const {
  75. return ( --m_map.upper_bound(key) )->second;
  76. }
  77. };
  78.  
  79. // Many solutions we receive are incorrect. Consider using a randomized test
  80. // to discover the cases that your implementation does not handle correctly.
  81. // We recommend to implement a test function that tests the functionality of
  82. // the interval_map, for example using a map of unsigned int intervals to char.
  83.  
  84. You can download this source code here:
  85.  
  86. Your task is to implement the function assign. Your implementation is graded by these criteria in this order:
  87.  
  88. Type requirements are met: You must adhere to the specification of the key and value type given above.
  89. Correctness: Your program should produce a working interval_map with the behavior described above. In particular, pay attention to the validity of iterators. It is illegal to dereference end iterators. Consider using a checking STL implementation such as the one shipped with Visual C++ or GCC.
  90. Canonicity: The representation in m_map must be canonical.
  91. Running time: Imagine your implementation is part of a library, so it should be big-O optimal. In addition:
  92. Do not make big-O more operations on K and V than necessary, because you do not know how fast operations on K/V are; remember that constructions, destructions and assignments are operations as well.
  93. Do not make more than two operations of amortized O(log N), in contrast to O(1), running time, where N is the number of elements in m_map. Any operation that needs to find a position in the map "from scratch", without being given a nearby position, is such an operation.
  94. Otherwise favor simplicity over minor speed improvements.
  95.  
  96. You should not take longer than 9 hours, but you may of course be faster. Do not rush, we would not give you this assignment if it were trivial.
  97.  
  98. When you are done, please complete the form and click Compile. You can improve and compile solutions as often as you like.
  99.  
  100. Please submit your solution until 19:16 UTC.
  101.  
  102. Further instructions will be given once your code compiles correctly.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement